Hello,

I master Hibernate framework. The following difficulty arose.

There is a program that creates 2 objects of type Employee and 1 type of Department .

 public class App { public static void main(String[] args) { Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); Department department = new Department("java"); session.save(department); session.persist(new Employee("Jakab Gipsz",department)); session.persist(new Employee("Captain Nemo",department)); session.getTransaction().commit(); Query q = session.createQuery("From Employee "); List<Employee> resultList = q.list(); System.out.println("num of employess:" + resultList.size()); for (Employee next : resultList) { System.out.println("next employee: " + next); } } } 

And at the end of the console is the inscription:

num of employess:2 next employee: Employee [id=2, name=Jakab Gipsz, department=java] next employee: Employee [id=3, name=Captain Nemo, department=java]

After checking the database (using apache derby) there is all this data there.

Further, as an experiment, I want to extract the data and this time I run the program already:

  public class App { public static void main(String[] args) { Session session = HibernateUtil.getSessionFactory().openSession(); Query q = session.createQuery("From Employee "); List<Employee> resultList = q.list(); System.out.println("num of employess:" + resultList.size()); for (Employee next : resultList) { System.out.println("next employee: " + next); } } } 

The program in the console issues

num of employess:0

And after the execution of this program, the table created earlier in the database is erased.

Please tell me why I can not extract the data in the 2nd session, and how to do it correctly so that the data are not erased?

==============================

Settings file "hibernate.cfg.xml":

 <property name="connection.url"> jdbc:derby://localhost:1527/Test;create=true;user=dd;password=123 </property> <property name="connection.username">dd</property> <property name="connection.password">123</property> <property name="dialect"> org.hibernate.dialect.DerbyDialect </property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <property name="current_session_context_class">thread</property> <mapping class="com.mastertheboss.domain.Employee" /> <mapping class="com.mastertheboss.domain.Department" /> 

Employee class:

 @Entity @Table public class Employee { @Id @GeneratedValue private Long id; private String name; @ManyToOne private Department department; public Employee() {} public Employee(String name, Department department) { this.name = name; this.department = department; } public Employee(String name) { this.name = name; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", department=" + department.getName() + "]"; } } 
  • one
    What is the value of the hibernate.hbm2ddl.auto parameter? And specify the configuration file Hiberneita. - MrFylypenko
  • @MrFylypenko, I don’t have such a file. I added that there is. This is the hibernate.cfg.xml configuration file and the Employee class. - foxis

1 answer 1

You have a parameter in the hibernate.cfg.xml file

 <property name="hbm2ddl.auto">create</property> 

it means that the tables will be created every time you start a project.

You can replace create with update and then the tables in the database will not be recreated, but will be updated with the current settings of the entities, i.e. adding columns, new tables (all the data in the table should remain). Or after the first launch of the application, comment out this parameter.

  • Thank you so much for the detailed answer. Indeed, after fixing with create -> update table has ceased to be erased. - foxis