chapter 01
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="connection.username">system</property> <property name="connection.password">orcl</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">2</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <property name="hbm2ddl.auto">create</property> <property name="current_session_context_class">thread</property> <mapping resource="com/demo/manyToMany/Project.hbm.xml"/> <mapping resource="com/demo/manyToMany/Employee.hbm.xml"/> </session-factory> </hibernate-configuration>
package com.demo.manyToMany; import java.util.HashSet; import java.util.Set; public class Employee { public Employee(){} public Employee(int id, String name, Set<Project> projects) { super(); this.id = id; this.name = name; this.projects = projects; } private int id; private String name; private Set<Project> projects = new HashSet<Project>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Project> getProjects() { return projects; } public void setProjects(Set<Project> projects) { this.projects = projects; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.demo.manyToMany"> <class name="Employee" table="Employee"> <id name="id"> <generator class="sequence"> <param name="sequence">seq_projectSys_employee</param> </generator> </id> <property name="name"/> <set name="projects" table="EmployeeProject" inverse="true"> <key column="employee_id" /> <many-to-many class="Project" column="project_id" /> </set> </class> </hibernate-mapping>
package com.demo.manyToMany; import java.util.HashSet; import java.util.Set; public class Project { public Project(){} public Project(int id, String name, Set<Employee> employees) { super(); this.id = id; this.name = name; this.employees = employees; } private int id; private String name; private Set<Employee> employees = new HashSet<Employee>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Employee> getEmployees() { return employees; } public void setEmployees(Set<Employee> employees) { this.employees = employees; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.demo.manyToMany"> <class name="Project" table="Project"> <id name="id"> <generator class="sequence"> <param name="sequence">seq_projectSys_project</param> </generator> </id> <property name="name"/> <set name="employees" table="EmployeeProject"> <key column="project_id" /> <many-to-many class="Employee" column="employee_id" /> </set> </class> </hibernate-mapping>