hibernate中one-to-one两种配置方式 灰太狼 2022-07-16 09:08 152阅读 0赞 public class Person { private int id; private String name; private IdCard idCard; public Person() { } public Person(int id, String name, IdCard idCard) { this.id = id; this.name = name; this.idCard = idCard; } 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; } } [java] view plain copy public class IdCard { private int id; private Date usefullLife; private Person person; public IdCard() { } public IdCard(int id, Date usefullLife, Person person) { this.id = id; this.usefullLife = usefullLife; this.person = person; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Date getUsefullLife() { return usefullLife; } public void setUsefullLife(Date usefullLife) { this.usefullLife = usefullLife; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } } one-to-one方式一: ![Center][] IdCard.hbm.xml <?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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.huawei.domain.IdCard" table="id_card"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="foreign" > <param name="property">person</param> </generator> </id> <property name="usefullLife" type="java.util.Date"> <column name="usefull_life"/> </property> <one-to-one name="person" constrained="true"></one-to-one> </class> </hibernate-mapping> Person.hbm.xml <?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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.huawei.domain.Person"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="name" length="5" not-null="false" /> </property> <one-to-one name="idCard"></one-to-one> </class> </hibernate-mapping> one-to-one方式二: ![Center 1][] IdCard.hbm.xml <?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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.huawei.domain.IdCard" table="id_card"> <id name="id" type="java.lang.Integer"> <!-- <column name="id" /> <generator class="foreign" > <param name="property">person</param> </generator> --> <column name="id" /> <generator class="native" /> </id> <property name="usefullLife" type="java.util.Date"> <column name="usefull_life"/> </property> <!-- <one-to-one name="person" constrained="true"></one-to-one> --> <many-to-one name="person" column="person_id" unique="true"></many-to-one> </class> </hibernate-mapping> Person.hbm.xml <?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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.huawei.domain.Person"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="name" length="5" not-null="false" /> </property> <one-to-one name="idCard" property-ref="person"></one-to-one> </class> </hibernate-mapping> 测试: Session s = null; try{ s = HibernateSessionFactory.getSession(); // Person person = (Person)s.get(Person.class, id); // System.out.println(person.getIdCard().getUsefullLife()); IdCard idCard = (IdCard)s.get(IdCard.class, id); System.out.println(idCard.getPerson().getName()); } catch (HibernateException e) { e.printStackTrace(); } [Center]: /images/20220716/26335a94f29f47c4a718722cdf83a637.png [Center 1]: /images/20220716/edc861c3c3a34cb6986afa291e6643a5.png
还没有评论,来说两句吧...