1

我有两个这样的课程

package models;
import java.util.*;
import javax.persistence.*;
import play.db.jpa.*;

@Entity
@Table(name = "commitment_type_value")
public class CommittmentTypeValue extends Model{    
    @Id
    @Column(name = "id", nullable = false)
    public Long id;    
    @Column(name = "value", nullable = true)
    public String type;    
    @ManyToOne
    @JoinColumn(name="commitment_type_id")
    public CommitementType commitmentType;    
    public CommittmentTypeValue(){

    }
}

-------------


package models;

import java.util.*;

import javax.persistence.*;

import play.db.jpa.*;


/**
 *
 * @author hatem
 */
@Entity
@Table(name = "commitment_type")
public class CommitementType extends Model{

    @Id
    @Column(name = "id", nullable = false)
    public Long id;

    @Column(name = "type", nullable = true)
    public String type;

    @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="commitmentType")
    public List<CommittmentTypeValue> commitmentTypeValues;

    public CommitementType(){

    }
}

当我执行我的应用程序时,会出现此问题:

发生 JPA 错误(无法构建 EntityManagerFactory):从 models.CommittmentTypeValue 引用 models.CommitementType 的外键具有错误的列数。应该是 2

拜托,谁能告诉我怎么了?

4

3 回答 3

3

请检查您的外键列名,它应该与列名完全匹配。


编辑 如果您的问题仍未解决,请检查您的 persistence.xml 是否有

<property name="generateDdl" value="true" />

如果它已经存在,请检查您是否在生成表时遇到任何错误。

如果是,则清除表中的数据或在配置文件中添加 drop-and-create-tables 选项或更改代码如下

@ManyToOne(optional=true)
@JoinColumn(name="commitment_type_id", nullable = true)
public CommitementType commitmentType;

因为您可能在表中有旧数据,这可能会停止创建新表。

于 2012-12-12T08:42:34.050 回答
0

该错误听起来像您在 CommitementType 中的 Id 是复合的,因此您的外键必须包含两列。

包括 CommitementType 的代码。

于 2012-12-12T15:51:06.507 回答
0

您的多对一加入课堂中缺少参考列名称CommittmentTypeValue

@ManyToOne
@JoinColumn(name="commitment_type_id" referencedColumnName="id" )
public CommitementType commitmentType;  

同时指定目标实体

@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, targetEntity=CommittmentTypeValue.class, mappedBy="commitmentType")
public List<CommittmentTypeValue> commitmentTypeValues;
于 2012-12-12T08:59:06.430 回答