Hibernate 의 one - to - one 외부 키 연결 맵
6073 단어 one-to-one
다른 하 나 는 메 인 키 를 통 해 두 데이터 시트 의 메 인 키 가 같은 값 을 사용 하도록 제한 하 는 것 이다.
포 류
Person.java
package po;
public class Person {
private int id;
private String 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;
}
}
Card.java
package po;
public class Card {
private int id; // ID
private String number; //
private Person person; //
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
Card.hbm.xml
<hibernate-mapping>
<class name="po.Card" table="card">
<id name="id" type="integer">
<generator class="native" />
</id>
<property name="number"></property>
<!-- unique=true -->
<many-to-one name="person" unique="true" column="person"></many-to-one>
</class>
</hibernate-mapping>
Person.hbm.xml
<hibernate-mapping>
<class name="po.Person" table="person">
<id name="id" type="integer">
<generator class="native" />
</id>
<property name="name" />
</class>
</hibernate-mapping>
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">1</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="myeclipse.connection.profile">mysql</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="po/Person.hbm.xml"/>
<mapping resource="po/Card.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Test.java
package po;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
public class PersonTest {
@Test
public void testCreateDB(){
//
Configuration config = new Configuration().configure();
SchemaExport export = new SchemaExport(config);
export.create(true, true);
}
@Test
public void testSave(){
//
Configuration config = new Configuration().configure();
SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
Person person = new Person(); // person
person.setName("zhangsan");
session.save(person); // person
Card card = new Card();
card.setNumber("4114031111222223333");
card.setPerson(person); //
session.save(card); // card
session.getTransaction().commit();
}
@Test
public void testGetPerson(){
Configuration config = new Configuration().configure();
SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
Card card = (Card)session.get(Card.class,2 ); // Card
System.out.println("CardNumber: "+card.getNumber());
Person person = card.getPerson(); // card.getPerson() person
System.out.println("PersonName: "+person.getName()); // person name
session.getTransaction().commit();
session.close();
}
}
person 과 card 는 각각 sql 을 생 성 합 니 다.
| card | CREATE TABLE `card` (
`id` int(11) NOT NULL auto_increment,
`number` varchar(255) default NULL,
`person` int(11) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `person` (`person`),
KEY `FK2E7B10BD1DCD99` (`person`),
CONSTRAINT `FK2E7B10BD1DCD99` FOREIGN KEY (`person`) REFERENCES `person` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
| person | CREATE TABLE `person` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |