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() + "]"; } }
hibernate.hbm2ddl.autoparameter? And specify the configuration file Hiberneita. - MrFylypenkohibernate.cfg.xmlconfiguration file and theEmployeeclass. - foxis