1.学生类:Student.java
package org.yang.hibernate.model;
public class Student
{
private String ID;
private String name;
private IDCard IDCard;
public Student(){}
public String getID() {
return ID;
}
public String getname() {
return name;
}
public IDCard getIDCard() {
return IDCard;
}
public voID setID(String ID) {
this.ID = ID;
}
public voID setname(String name) {
this.name = name;
}
public voID setIDCard(IDCard IDCard) {
this.IDCard = IDCard;
}
}
2.身份z类:IDCard.java
package org.yang.hibernate.model;
public class IDCard
{
private String ID;
private int number;
private Student student;
public IDCard(){}
public String getID() {
return ID;
}
public int getNumber() {
return number;
}
public Student getStudent() {
return student;
}
public voID setID(String ID) {
this.ID = ID;
}
public voID setNumber(int number) {
this.number = number;
}
public voID setStudent(Student student) {
this.student = student;
}
}
3.学生类的映射文件:Student.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" >
<hibernate-mapPing>
<class name="org.yang.hibernate.model.Student" table="student">
<ID name="ID" column="ID" type="string">
<generator ></generator>
</ID>
<property name="name" column="name" type="string"></property>
<one-to-one name="IDCard" cascade="all" fetch="join">
</one-to-one>
</class>
</hibernate-mapPing>
4.身份z类的映射文件: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" >
<hibernate-mapPing>
<class name="org.yang.hibernate.model.IDCard" table="IDcard">
<ID name="ID" column="ID">
<generator >
<!--
根据当前类IDCard的哪一个属性的名字来生成.
在Student的hbm根据IDCard来生成也是可以的。
这根据个人喜好
-->
<param name="property">student</param>
</generator>
</ID>
<property name="number" column="number" type="integer"></property>
<one-to-one name="student" ></one-to-one>
<!--
外键关联,本质上就是一对多的蜕化形式。在many-to-one元素中增加unique="true"就成了一对一
当修改成这个样子时,也不要忘记了将<ID>元素的主键生成方式也改掉。改为:
<ID name="ID" column="ID">
<generator />
</ID>
<many-to-one name="student" unique="true" column="student_ID"/>
-->
</class>
</hibernate-mapPing>
5.总配置文件: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>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/hibernate</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">yang</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgresqlDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapPing resource="IDCard.hbm.xml"/>
<mapPing resource="Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6.生成数据库表的代码文件:Createtable.java
package org.yang.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class Createtable
{
public static voID main(String[] args)
{
SchemaExport export = new SchemaExport(new Configuration().configure());
export.create(true,true);
}
}
7.测试文件:HibernateTest.java
package org.yang.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.yang.hibernate.model.IDCard; import org.yang.hibernate.model.Student; public class HibernateTest { private static final SessionFactory sessionFactory; static { try { sessionFactory = new Configuration().configure().buildSessionFactory(); } catch(Throwable ex) { System.err.println("Initial SessionFactory Failed " + ex); throw new ExceptionInInitializerError(ex); } } public static voID main(String[] args) { Student student = new Student(); student.setname("yangzhiyong"); IDCard IDCard = new IDCard(); IDCard.setNumber(987654); student.setIDCard(IDCard); IDCard.setStudent(student); Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.save(student); tx.commit(); } catch (Exception e) { if (null != tx) { tx.rollback(); } e.printstacktrace(); } finally { session.close(); } } }
总结以上是内存溢出为你收集整理的主键共享方式和外键方式一对一双向关系映射(参考张龙老师的例子)全部内容,希望文章能够帮你解决主键共享方式和外键方式一对一双向关系映射(参考张龙老师的例子)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)