OneToOne与复合键

时间:2015-11-12 14:38:12

标签: java spring hibernate jpa

尝试使用复合键在表上创建一对一。 我无法让它工作并收到此错误:

在类路径资源[applicationContext-dao.xml]中定义名为'sessionFactory'的bean时出错:init方法的调用失败;嵌套异常是org.hibernate.MappingException:已损坏的列映射:compensation.id:com.ciwise.model.Focus

Compensation.java:

package com.ciwise.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name = "commissions")
public class Compensation  implements Serializable {

private static final long serialVersionUID = 1L;

/**
 * Composite key
 */
private CompensationPK compensationPK;

/**
 * This year monthly net sales
 */
private double tYMonthlyNetSales;

/**
 * Last year monthly net sales
 */

private double lYMonthlyNetSales;

/**
 * This year YTD net sales
 */
private double tYYTDNetSales;

private Focus focus;

/**
 * Getters and Setters
 */

@OneToOne( mappedBy = "compensation", fetch = FetchType.EAGER)
@JoinColumn(name = "FOCUS_ID")
public Focus getFocus() {
    return focus;
}

public void setFocus(Focus focus) {
    this.focus = focus;
}

@EmbeddedId
public CompensationPK getCompensationPK() {
    return compensationPK;
}

public void setCompensationPK(CompensationPK compensationPK) {
    this.compensationPK = compensationPK;
}

@Column(name = "TY_MONTHLY_NET_SALES")
public double gettYMonthlyNetSales() {
    return tYMonthlyNetSales;
}

public void settYMonthlyNetSales(double tYMonthlyNetSales) {
    this.tYMonthlyNetSales = tYMonthlyNetSales;
}

@Column(name = "LY_MONTHLY_NET_SALES")
public double getlYMonthlyNetSales() {
    return lYMonthlyNetSales;
}

public void setlYMonthlyNetSales(double lYMonthlyNetSales) {
    this.lYMonthlyNetSales = lYMonthlyNetSales;
}

@Column(name = "TY_YTD_NET_SALES")
public double gettYYTDNetSales() {
    return tYYTDNetSales;
}

public void settYYTDNetSales(double tYYTDNetSales) {
    this.tYYTDNetSales = tYYTDNetSales;
}
}

CompensationPK.java

package com.ciwise.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class CompensationPK implements Serializable {

private String divisionId;

private String repId;

private int focusId;

private int repTypeId;

private int commissionYear;

private int commissionMonth;

@Column(name = "DIVISION_ID")
public String getDivisionId() {
    return divisionId;
}

@Column(name = "REP_ID")
public String getRepId() {
    return repId;
}

@Column(name = "FOCUS_ID")
public int getFocusId() {
    return focusId;
}

@Column(name = "REPTYPE_ID")
public int getRepTypeId() {
    return repTypeId;
}

@Column(name = "COMMISSION_YEAR")
public int getCommissionYear() {
    return commissionYear;
}

@Column(name = "COMMISSION_MONTH")
public int getCommissionMonth() {
    return commissionMonth;
}



public void setDivisionId(String divisionId) {
    this.divisionId = divisionId;
}

public void setRepId(String repId) {
    this.repId = repId;
}

public void setFocusId(int focusId) {
    this.focusId = focusId;
}

public void setRepTypeId(int repTypeId) {
    this.repTypeId = repTypeId;
}

public void setCommissionYear(int commissionYear) {
    this.commissionYear = commissionYear;
}

public void setCommissionMonth(int commissionMonth) {
    this.commissionMonth = commissionMonth;
}

@Override
public boolean equals(Object o) {
    return false;
}

@Override
public int hashCode() {
    return 0;
}
}

Focus.java:

包com.ciwise.model;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "CT_FOCUS")
public class Focus implements Serializable {

private int focusId;
private String focusDesc;
private String focusYN;
private Compensation compensation;

@OneToOne
@PrimaryKeyJoinColumn
public Compensation getCompensation() {
    return compensation;
}

public void setCompensation(Compensation compensation) {
    this.compensation = compensation;
}

public Focus() {
};

@Id
@Column(name = "FOCUS_ID")
public int getFocusId() {
    return focusId;
}

public void setFocusId(int focusId) {
    this.focusId = focusId;
}

@Column(name = "FOCUS_DESC", length = 16)
public String getFocusDesc() {
    return focusDesc;
}

public void setFocusDesc(String focusDesc) {
    this.focusDesc = focusDesc;
}

@Column(name = "FOCUS_YN", length = 1)
public String getFocusYN() {
    return focusYN;
}

public void setFocusYN(String focusYN) {
    this.focusYN = focusYN;
}
}

1 个答案:

答案 0 :(得分:1)

由于您使用了可嵌入类型(CompositionPK)作为Composition实体的主键,因此您应使用Composition@EmbeddedId实体中添加相应的主键字段

@EmbeddedId
private CompensationPK compensationPK;

Focus实体上,您无需在一对一映射上指定@PrimaryKeyJoinColumn。它只使用外键的默认连接列名称。

所以这个代码应该没有@PrimaryKeyJoinColumn

@OneToOne
public Compensation getCompensation() {
    return compensation;
}

这是一个基于映射的Hibernate生成模式示例(目标数据库是MySQL):

Hibernate: 
    create table CT_FOCUS (
        FOCUS_ID integer not null,
        FOCUS_DESC varchar(16),
        FOCUS_YN varchar(1),
        compensation_COMMISSION_MONTH integer,
        compensation_COMMISSION_YEAR integer,
        compensation_DIVISION_ID varchar(255),
        compensation_FOCUS_ID integer,
        compensation_REP_ID varchar(255),
        compensation_REPTYPE_ID integer,
        primary key (FOCUS_ID)
    )
Hibernate: 
    create table commissions (
        COMMISSION_MONTH integer not null,
        COMMISSION_YEAR integer not null,
        DIVISION_ID varchar(255) not null,
        FOCUS_ID integer not null,
        REP_ID varchar(255) not null,
        REPTYPE_ID integer not null,
        LY_MONTHLY_NET_SALES double precision,
        TY_MONTHLY_NET_SALES double precision,
        TY_YTD_NET_SALES double precision,
        primary key (COMMISSION_MONTH, COMMISSION_YEAR, DIVISION_ID, FOCUS_ID, REP_ID, REPTYPE_ID)
    )
Hibernate: 
    alter table CT_FOCUS 
        add constraint FK_d6d2c9n91dlw59uiuqswfueg5 
        foreign key (compensation_COMMISSION_MONTH, compensation_COMMISSION_YEAR, compensation_DIVISION_ID, compensation_FOCUS_ID, compensation_REP_ID, compensation_REPTYPE_ID) 
        references commissions (COMMISSION_MONTH, COMMISSION_YEAR, DIVISION_ID, FOCUS_ID, REP_ID, REPTYPE_ID)
如果您希望@PrimaryKeyJoinColumn实体的主键引用@OneToOne实体的主键,则可以在Focus映射上使用

Commission。但是,您已经为Focus实体定义了一个主键,focusId注释了@Id。因此,无需指定@PrimaryKeyJoinColumn