This explanation based on the associated one-way foreign key mapping
1. examine the following information is a one to one relationship between human identity card. Table design
Note: Based on the table structure and many-to-one association table structure of the foreign key is consistent, however, the foreign key is unique.
2. The structure of the class
Person.java
public class Person implements Serializable{ private int id; private String name; private IdCard idCard; public Person() { } public Person(String name) { super(); this.name = name; } 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 IdCard getIdCard() { return idCard; } public void setIdCard(IdCard idCard) { this.idCard = idCard; } }
IdCard.java
public class IdCard implements Serializable{ private String id; private String address; public IdCard() { // TODO Auto-generated constructor stub } public IdCard(String id, String address) { super(); this.id = id; this.address = address; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
3. Mapping file information:
IdCard.hbm.xml
<hibernate-mapping package="cn.sxt.pojo"> <class name="IdCard" table="t_idCard"> <id name="id"> <generator class="assigned"></generator> </id> <property name="address"/> </class> </hibernate-mapping>
Person.hbm.xml
<-Hibernate Mapping Package = "cn.sxt.pojo"> < class name = " the Person " Table = "t_person"> <name ID = "ID"> <Generator class = "Native"> </ Generator> </ ID > <Property name = "name" /> <-! on-one and many-to-foreign key table structure is the same, so the mapping file is the same, to ensure that the foreign key is unique -> < MANY One--to name = "idCard" column = "idCardId" UNIQUE = "to true" > </ MANY-to-One> </ class > </ Hibernate-Mapping>
4. Test
public class HibernateTest { / ** * means a method for generating database tables * * / @Test public void testCreateDB () { the Configuration CFG = new new the Configuration () Configure ();. the SchemaExport SE = new new the SchemaExport (CFG); // first sql script print parameters whether // if the second parameter to the database exported script execution se.create ( to true , to true ); } / ** * initialization table data * / @Test public void testInit () { Session session = null; Transaction tx = null; try { session = HibernateUtil.getSession(); tx = session.beginTransaction(); IdCard card1 = new IdCard("110086", "湖北武当"); IdCard card2 = new IdCard("110087", "光明顶"); Person p1 = new Person("张三疯"); p1.setIdCard(card1); Person p2 = new Person("殷素素"); p2.setIdCard(card2); session.save(card1); session.save(card2); session.save(p1); session.save(p2); tx.commit(); } catch (Exception e) { if(tx!=null) tx.rollback(); }finally { HibernateUtil.close(); } } /** */ @Test public void testGetData(){ Session session = HibernateUtil.getSession(); Person p1 = (Person)session.get(Person.class, 1); System.out.println(p1.getName()+"----"+p1.getIdCard().getId()+"----"+p1.getIdCard().getAddress()); HibernateUtil.close(); } }