项目结构:
Wife
package auth.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="wife")
public class Wife {
private int id;
private String name;
private int age;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="age")
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Husband
package auth.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="husband")
public class Husband {
private int id;
private String name;
private int age;
private Wife wife;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="age")
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//注意:这个husband的id要和wife的id一样,表的主键同时是另一张表的外键,不然无法关联
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY,targetEntity=Wife.class)
@JoinColumn(name="id", unique=true, nullable=false, updatable=false)
public Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}
DAO:
package auth.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import auth.model.Husband;
public interface HusbandRepository extends JpaRepository<Husband, Integer>{
}
package auth.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import auth.model.Wife;
public interface WifeRepository extends JpaRepository<Wife, Integer>{
}
UserController
package auth.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import auth.dao.HusbandRepository;
import auth.dao.WifeRepository;
import auth.model.Husband;
import auth.model.Wife;
@Service
public class UserController {
private Logger log=LoggerFactory.getLogger(this.getClass());
@Autowired
private HusbandRepository husDao;
@Autowired
private WifeRepository wifeDao;
public void addUser(){
Husband hus=new Husband();
hus.setAge(23);
hus.setName("wy");
Wife wife=new Wife();
wife.setName("xxt");
wife.setAge(22);
hus.setWife(wife);
husDao.save(hus);
}
public void deleteUser(){
husDao.delete(6);
}
public void updateUser(){
Husband hus=husDao.findOne(7);
hus.setName("ws");
hus.getWife().setName("updatelcy");
husDao.save(hus);
}
public String queryUser(){
Husband husband=husDao.findOne(8);
return JSONObject.toJSONString(husband);
}
}
Tests
package spring;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import auth.controller.UserController;
public class Tests extends Base{
@Autowired
private UserController con;
@Test
public void addUser(){
con.addUser();
}
@Test
public void deleteUser(){
con.deleteUser();
}
@Test
public void updateUser(){
con.updateUser();
}
@Test
public void queryUser(){
String result=con.queryUser();
System.out.println(result);
}
}
运行过程:
add
2017-05-25 12:59:21,930 DEBUG [org.hibernate.SQL:109] -
insert
into
husband
(age, name)
values (?, ?) 2017-05-25 12:59:21,981 DEBUG [org.hibernate.id.IdentifierGeneratorHelper:94] - Natively generated identity: 6 2017-05-25 12:59:21,990 DEBUG [org.hibernate.engine.spi.ActionQueue:196] - Executing identity-insert immediately 2017-05-25 12:59:21,991 DEBUG [org.hibernate.SQL:109] - insert into wife (age, name) values (?, ?)
delete
delete
from
wife
where
id=?
2017-05-25 14:55:37,618 DEBUG [org.hibernate.SQL:109] - delete from husband where id=?
update
update
wife
set
age=?,
name=?
where id=? 2017-05-25 14:46:32,037 DEBUG [org.hibernate.SQL:109] - update husband set age=?, name=? where id=?
query
select
husband0_.id as id1_0_0_,
husband0_.age as age2_0_0_,
husband0_.name as name3_0_0_
from
husband husband0_
where
husband0_.id=?
2017-05-25 14:48:24,685 DEBUG [org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl:127] - Starting ResultSet row #0 2017-05-25 14:48:24,687 DEBUG [org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl:142] - On call to EntityIdentifierReaderImpl#resolve, EntityKey was already known; should only happen on root returns with an optional identifier specified 2017-05-25 14:48:24,705 DEBUG [org.hibernate.engine.internal.TwoPhaseLoad:160] - Resolving associations for [auth.model.Husband#8] 2017-05-25 14:48:24,708 DEBUG [org.hibernate.SQL:109] - select wife0_.id as id1_1_0_, wife0_.age as age2_1_0_, wife0_.name as name3_1_0_ from wife wife0_ where wife0_.id=?
查询运行结果:
{"age":23,"id":8,"name":"wy","wife":{"age":22,"id":8,"name":"xxt"}}