hibernate1对一外键关联(单向)
hibernate1对1外键关联(单向)
wife.java
hibernate.cfg.xml
23:54:47,765 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
drop table if exists Student
23:54:47,765 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
create table StuIdCard (
id integer not null,
num varchar(255),
primary key (id)
)
23:54:47,859 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
create table Student (
id integer not null auto_increment,
name varchar(255),
age integer,
sex varchar(255),
good char(1),
primary key (id)
)
23:54:47,968 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
alter table StuIdCard
add index FKD3A449FFEC65FB66 (id),
add constraint FKD3A449FFEC65FB66
foreign key (id)
references Student (id)
23:54:48,156 INFO org.hibernate.tool.hbm2ddl.SchemaExport:268 - schema export complete[/img]
引用
注解形式
package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; @Entity public class Husband { private int id; private String name; private Wife wife; @Id @GeneratedValue public int getId() { return id; } public String getName() { return name; } @OneToOne @JoinColumn(name="wifeId") public Wife getWife() { return wife; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setWife(Wife wife) { this.wife = wife; } }
wife.java
package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Wife { private int id; private String name; @Id @GeneratedValue 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; } }
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">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">bjsxt</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:SXT</property> <property name="connection.username">scott</property> <property name="connection.password">tiger</property> <property name="dialect">org.hibernate.dialect.OracleDialect</property> --> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- Drop and re-create the database schema on startup <property name="hbm2ddl.auto">update</property> --> <!-- --> <mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/> <mapping resource="com/bjsxt/hibernate/StuIdCard.hbm.xml"/> <mapping class="com.bjsxt.hibernate.Husband"/> <mapping class="com.bjsxt.hibernate.Wife"/> </session-factory> </hibernate-configuration>
引用
xml形式
package com.bjsxt.hibernate; public class StuIdCard { private int id; private String num; private Student student; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } }
package com.bjsxt.hibernate; public class Student { private int id; private String name; private int age; private String sex; private boolean good; public boolean isGood() { return good; } public void setGood(boolean good) { this.good = good; } 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.bjsxt.hibernate.Student" dynamic-update="true"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <property name="age" /> <property name="sex" /> <property name="good" type="yes_no"></property> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.bjsxt.hibernate.StuIdCard"> <id name="id"> <generator class="native"></generator> </id> <property name="num"/> <many-to-one name="student" column="studentId" unique="true"></many-to-one> </class> </hibernate-mapping>
引用
测试
public class TestHibernate extends TestCase { private static SessionFactory sessionFactory; /* (non-Javadoc) * @see junit.framework.TestCase#setUp() */ protected void setUp() throws Exception { sessionFactory = new AnnotationConfiguration().configure() .buildSessionFactory(); } /* (non-Javadoc) * @see junit.framework.TestCase#tearDown() */ protected void tearDown() throws Exception { sessionFactory.close(); } public void testSchemaExport() { new SchemaExport(new AnnotationConfiguration().configure()).create( false, true); } }[img] drop table if exists StuIdCard
23:54:47,765 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
drop table if exists Student
23:54:47,765 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
create table StuIdCard (
id integer not null,
num varchar(255),
primary key (id)
)
23:54:47,859 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
create table Student (
id integer not null auto_increment,
name varchar(255),
age integer,
sex varchar(255),
good char(1),
primary key (id)
)
23:54:47,968 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
alter table StuIdCard
add index FKD3A449FFEC65FB66 (id),
add constraint FKD3A449FFEC65FB66
foreign key (id)
references Student (id)
23:54:48,156 INFO org.hibernate.tool.hbm2ddl.SchemaExport:268 - schema export complete[/img]