JPA在具有不同列名的复合键之间映射OneToMany

时间:2017-06-16 12:08:14

标签: mysql hibernate jpa

我尝试从LEGACY数据库模式中创建一个jpa模型。

我在mysql中创建了一个简单的数据库,我在eclipse(Reverse)中使用jpa工具生成了模型。

jpa工具生成的代码在我的springboot项目中出错:

org.springframework.beans.factory.BeanCreationException:创建名为'entityManagerFactory'的bean时出错 在类路径资源中定义[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaAutoConfiguration.class]:

调用init方法失败;嵌套异常是org.hibernate.AnnotationException:stackoverflow.entity.SecondTable.mainTable的referencedColumnNames(id) 引用stackoverflow.entity.MainTable未映射到单个属性

我认为这是有道理的,因为在SecondTable中我的PK是(id,day),而在MainTable中是(id,year,month,code)。 现在的想法是让id与之相关。

几个链接: https://gigsterous.github.io/engineering/2016/09/25/spring-boot-2.html

对于属性覆盖(可能指定哪个列不相关) http://techqa.info/programming/question/33620473/hibernate-bidirectional-one-to-many-with-composite-key

JPA: Entity mapping with composite primary key and single key

这些是由jpa工具创建的4个实体:

@Entity
@Table(name="main_table")
public class MainTable implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private MainTablePK id;

    private String comment;

    //bi-directional many-to-one association to SecondTable
    @OneToMany(mappedBy="mainTable")
    private List<SecondTable> secondTables;

    public MainTable() {
    }

}

@Embeddable
public class MainTablePK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    private int id;

    private String year;

    private String month;

    private String code;

    public MainTablePK() {
    }
}

@Entity
@Table(name="second_table")
public class SecondTable implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private SecondTablePK id;


    //bi-directional many-to-one association to MainTable

    //I think I don’t need a bi-directional relationship, just from MainTable to SecondTable

    //ERROR: NOT MAPPED TO A SINGLE PROPERTY
    //I understand id in MainTable is a composite Key
    @ManyToOne
    @JoinColumn(name="id", referencedColumnName="id")
    private MainTable mainTable;


    public SecondTable() {
    }
}


@Embeddable
public class SecondTablePK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    private int id;

    private int day;

    private int value1;

    private int value2;

    private int value3;

    public SecondTablePK() {
    }
}

SCRIPT SQL和数据示例

DROP TABLE IF EXISTS `main_table`;
CREATE TABLE `main_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `year` char(4) NOT NULL,
  `month` char(2) NOT NULL,
  `code` char(6) NOT NULL,
  `comment` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`,`year`,`month`,`code`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

INSERT INTO `main_table` VALUES (1,'2017','01','333090','coment one');

This has a composite key

DROP TABLE IF EXISTS `second_table`;
CREATE TABLE `second_table` (
  `id` int(11) NOT NULL,
  `day` int(11) NOT NULL,
  `value1` int(11) DEFAULT NULL,
  `value2` int(11) DEFAULT NULL,
  `value3` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`,`day`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


INSERT INTO `second_table` VALUES (1,0,1,NULL,NULL),(1,1,2,NULL,NULL),(1,2,NULL,NULL,NULL),(1,3,NULL,NULL,NULL),(1,4,NULL,NULL,NULL),(1,32,NULL,NULL,NULL);

可以使用JPA实现吗? 有什么建议吗?

由于

更新

删除了非PK字段value1,value2和value3,这是一个错字。

遵循JPA 2.1规范,第2.4.3节,示例2,第34,35页与我的非常相似。

所以申请:

@MapsId("empPK")
              @JoinColumns({
@JoinColumn(name="FK1", referencedColumnName="firstName"),
@JoinColumn(name="FK2", referencedColumnName="lastName") })

我们有:

@ManyToOne
@MapsId("id") // maps the 'id' attribute of the embedded ID
@JoinColumn(name="id", referencedColumnName="id")
private MainTable mainTable;

不再进行任何更改。

我收到下一个错误: org.springframework.beans.factory.BeanCreationException:在类路径资源[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaAutoConfiguration.class]中定义名称为'entityManagerFactory'的bean创建错误:init方法的调用失败;嵌套异常是org.hibernate.AnnotationException:无法在@MapsId映射中找到列引用:代码

如果我删除代码,同样的错误,但,如果我删除月份,则与相同的错误。

由于

1 个答案:

答案 0 :(得分:0)

首先,您的SecondTablePK似乎不正确,因为它包含非PK字段value1value2value3。它应该看起来像这样:

@Embeddable
public class SecondTablePK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    private int id;

    private int day;

    public SecondTablePK() {
    }
}

关于共享id:通常,从属实体将引用父实体的整个主键,但您可以像这样映射关系:

@Entity
@Table(name="second_table")
public class SecondTable implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private SecondTablePK id;

    private int value1;

    private int value2;

    private int value3;

    @ManyToOne
    @MapsId("id") // maps the 'id' attribute of the embedded ID
    @JoinColumn(name="id", referencedColumnName="id")
    private MainTable mainTable;


    public SecondTable() {
    }
}

这就像“派生身份”,在JPA 2.1规范第2.4.1节中讨论过。