SudentsPk.java ackage com.mao;
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import org.hibernate.annotations.GenericGenerator;
//身份证类 @Entity public class StudentsPk { @Id @GeneratedValue(generator="Pk") @GenericGenerator(name="Pk",strategy="assigned") @Column(length=10) private String Pk; private String name;
<!--双向关联关系 @OneToOne(mappedBy="idcard") private Students st; public Students getSt() { return st; } public void setSt(Students st) { this.st = st; } -->
public StudentsPk(){
}
public StudentsPk(String pk, String name) { super(); Pk = pk; this.name = name; }
public String getPk() { return Pk; }
public void setPk(String pk) { Pk = pk; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
2 Studnets.java package com.mao;
import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne;
import org.hibernate.type.TrueFalseType;
@Entity(name="hu_studnets") public class Students {
private int sid;//学号 private String phone; private String post; private StudentsPk idcard; public Students(){
}
public Students(int sid, String phone, String post,StudentsPk idcard) { super(); this.sid = sid; this.phone = phone; this.post = post; this.idcard=idcard; } @Id @GeneratedValue public int getSid() { return sid; }
public void setSid(int sid) { this.sid = sid; }
public String getPhone() { return phone; }
public void setPhone(String phone) { this.phone = phone; }
public String getPost() { return post; }
public void setPost(String post) { this.post = post; }
@OneToOne(cascade=CascadeType.ALL) @JoinColumn(name="Pk",unique=true) public StudentsPk getIdcard() { return idcard; }
public void setIdcard(StudentsPk idcard) { this.idcard = idcard; }
}
3 hibernte.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="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql://127.0.0.1:3306/maodongbang?useUnicode=true&characterEncoding=UTF-8 </property> <property name="connection.username">root</property> <property name="connection.password">mft131415138</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.mao.Students"/>
<mapping class="com.mao.StudentsPk"/>
</session-factory>
</hibernate-configuration>
4Test.java package com.mao;
import javax.xml.validation.Schema;
import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.tool.hbm2ddl.SchemaExport;
public class Test { @org.junit.Test public void test(){ Configuration config=new Configuration().configure(); ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry); SchemaExport export=new SchemaExport(config); export.create(true,true); } }