一. 整理JPA关系映射(双向一对一)

一对一有关要点:

@OneToOne : 既然是双向,两边都应有这个注解;

mappedBy : 这个是最容易搞混淆的.在一对一数据库对应关系中,我们会定义一个主表和一个从表,主表会有一个外键被从表引用.这时我们则要用mappedBy 来定义哪个是主表;

cascade : 当前对象发生事件时,被引用的对象将要发生的事件.当我们在主表中定义 cascade = CascadeType.REMOVE 时,表明主表删除时,从表中的数据也会被删除.

@JoinColumn : 主要是定义在从表,因为从表要引用主表的一个外键,可以用这个注解要定义外键的名称

optional : 这是@OneToOne注解中的一个属性,表示当前对象在保存时,该属性是否可为空.

下面主表SystemAccount 与从表SystemAccountBasicInfo的关系 

package org.pan.domain.db;

import org.pan.domain.DomainObject;

import javax.persistence.*;

/**
 * 登陆用户基本信息
 * Created by fangjinliu on 2015/10/29 0029.
 */
@Entity
public class SystemAccount extends DomainObject{

    @Column(unique = true)
    private String username;
    @Column
    private String password;
    @OneToOne(mappedBy = "systemAccount",cascade = CascadeType.REMOVE)
    private SystemAccountBasicInfo systemAccountBasicInfo;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public SystemAccountBasicInfo getSystemAccountBasicInfo() {
        return systemAccountBasicInfo;
    }

    public void setSystemAccountBasicInfo(SystemAccountBasicInfo systemAccountBasicInfo) {
        this.systemAccountBasicInfo = systemAccountBasicInfo;
    }

    @Override
    public String toString() {
        return "SystemAccount{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

 

package org.pan.domain.db;

import org.pan.domain.DomainObject;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

/**
 * 用户扩展信息
 * Created by fangjinliu on 2015/10/29 0029.
 */
@Entity
public class SystemAccountBasicInfo extends DomainObject{

    @Column
    private String nickName;
    @Column
    private String telPhone;
    @Column
    private String email;

    @OneToOne(optional = false)
    @JoinColumn(name = "systemAccount_id",referencedColumnName = "id")
    private SystemAccount systemAccount;

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getTelPhone() {
        return telPhone;
    }

    public void setTelPhone(String telPhone) {
        this.telPhone = telPhone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public SystemAccount getSystemAccount() {
        return systemAccount;
    }

    public void setSystemAccount(SystemAccount systemAccount) {
        this.systemAccount = systemAccount;
    }
}

单元测试的结果:

package org.pan.domain.db;

import org.junit.Assert;
import org.junit.Test;
import org.pan.domain.AbstractHibernateTest;

/**
 * 测试systemAccount中一对一关系
 * Created by fangjinliu on 2015/10/29 0029.
 */
public class SystemAccountTest extends AbstractHibernateTest {

    private SystemAccount createSystemAccount(String username, String password){
        SystemAccount systemAccount = new SystemAccount();
        systemAccount.setUsername(username);
        systemAccount.setPassword(password);
        return systemAccount;
    }

    @Test
    public void testSimpleSaveRemove() {
        SystemAccount systemAccount = createSystemAccount("username","123");
        save(systemAccount);
        Assert.assertNotNull(systemAccount.getId());

        remove(SystemAccount.class,systemAccount.getId());
        SystemAccount find = (SystemAccount) find(SystemAccount.class, systemAccount.getId());
        Assert.assertNull(find);
    }

    @Test
    public void testSaveSystemAccountBasicInfo(){
        SystemAccount systemAccount = createSystemAccount("username","123");
        save(systemAccount);

        SystemAccountBasicInfo systemAccountBasicInfo = createSystemAccountBasicInfo("microsoft","18589024202","[email protected]");
        systemAccountBasicInfo.setSystemAccount(systemAccount);
        save(systemAccountBasicInfo);
        Assert.assertNotNull(systemAccountBasicInfo.getId());

        SystemAccountBasicInfo findBasicInfo = (SystemAccountBasicInfo) find(SystemAccountBasicInfo.class,systemAccountBasicInfo.getId());
        Assert.assertNotNull(findBasicInfo);
        Assert.assertNotNull(findBasicInfo.getSystemAccount());

        SystemAccount findSystemAccount = (SystemAccount) find(SystemAccount.class,systemAccount.getId());
        Assert.assertNotNull(findSystemAccount);
        Assert.assertNotNull(findSystemAccount.getSystemAccountBasicInfo());

        remove(SystemAccount.class,findSystemAccount.getId());
    }

    private SystemAccountBasicInfo createSystemAccountBasicInfo(String nickName, String telPhone, String email) {
        SystemAccountBasicInfo systemAccountBasicInfo = new SystemAccountBasicInfo();
        systemAccountBasicInfo.setEmail(email);
        systemAccountBasicInfo.setNickName(nickName);
        systemAccountBasicInfo.setTelPhone(telPhone);
        return systemAccountBasicInfo;
    }

}

 

你可能感兴趣的:(一. 整理JPA关系映射(双向一对一))