CREATE DATABASE `onetoone`
CHARACTER SET 'utf8';
USE `onetoone`;
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `computer`;
CREATE TABLE `computer` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`student_id` int(11) ,
foreign key (`student_id`) references student(`id`),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
package com.domain;
public class Student implements java.io.Serializable {
private Integer id;
private String name;
private Computer computer;
public Student() {
}
public Student(String name) {
this.name = name;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Computer getComputer() {
return computer;
}
public void setComputer(Computer computer) {
this.computer = computer;
}
}
package com.domain;
public class Computer implements java.io.Serializable {
private Integer id;
private Student student;
private String name;
public Computer() {
}
public Computer(String name) {
this.name = name;
}
public Computer(Student student, String name) {
this.student = student;
this.name = name;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Student getStudent() {
return this.student;
}
public void setStudent(Student student) {
this.student = student;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<one-to-one cascade="delete,save-update" name="computer" class="com.domain.Computer" property-ref="student">one-to-one>
hibernate-mapping>
hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
hibernate-mapping>
package com.domain;
import org.hibernate.Query;
import org.hibernate.Session;
import com.util.HibernateSessionFactory;
public class Test {
public static void main(String[] args){
Session session = HibernateSessionFactory.getSession();
//save
Student student = new Student();
student.setName("student9");
Computer computer = new Computer();
computer.setName("Intel 9");
//computer.setStudent(student)和student.setComputer(computer);都必须要
computer.setStudent(student);
student.setComputer(computer);
session.save(student);
/**
执行的sql:
Hibernate: insert into onetoone.student (name) values (?)
Hibernate: insert into onetoone.computer (name, student_id) values (?, ?)
*/
//
session.beginTransaction().commit();
//query
// String hql = "from Student where name=?";
// Query query = session.createQuery(hql);
// query.setString(0, "student3");
//
// Student student = (Student)query.uniqueResult();
// System.out.println(student.getId() + " : " + student.getComputer().getName());
//delete
// Student student = (Student)session.load(Student.class, new Integer(1));
// session.delete(student);
// session.beginTransaction().commit();
/**
执行的sql为:
Hibernate: select student0_.id as id0_1_, student0_.name as name0_1_, computer1_.id as id1_0_, computer1_.name as name1_0_, computer1_.student_id as student3_1_0_ from onetoone.student student0_ left outer join onetoone.computer computer1_ on student0_.id=computer1_.student_id where student0_.id=?
Hibernate: delete from onetoone.computer where id=?
Hibernate: delete from onetoone.student where id=?
*/
session.close();
}
}
hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<one-to-one name="computer"/>
hibernate-mapping>
hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
student
<one-to-one name="student"/>
hibernate-mapping>
package com.domain;
import org.hibernate.Session;
import com.util.HibernateSessionFactory;
public class TestMainKey {
public static void main(String[] args){
Session session = HibernateSessionFactory.getSession();
//save
Student student = new Student();
student.setName("student15");
Computer computer = new Computer();
computer.setName("Intel 15");
computer.setStudent(student);
// student.setComputer(computer);
//因为save 2个表,所以不需要双向赋值
session.save(student);
session.save(computer);
session.beginTransaction().commit();
session.close();
}
}
DROP TABLE IF EXISTS `st`;
create table stu_com(
stu_id int not null,
com_id int not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;