当我输入ManyToOne注释时没有主键错误

时间:2016-04-24 17:18:36

标签: java mysql jpa persistence

我有这个层次结构

T1(
   id (pk),
   name (varchar)
)

T2(
  id (pk),
  t1_id (fk_t1),
  number (int)
)

T3 (
  id (pk),
  t2_id (fk_t2),
  time (datetime)
  zone (tinyint),
  name (varchar)
)

这是我的T3Entity

@Entity
@Table(name="T3, schema="", catalog="dbname")
public class T3Entity{
    private int id;
    private DateTime datetime;
    private int zone;
    private String name;

   @Id
   @Column(name="id", nullable=false, insertable=true, updatable=true)
   //GETTER/SETTERS

   @Basic
   @Column(name="datetime", nullable=false, insertable=true, updatable=true)
   //GETTER/SETTERS

   @Basic
   @Column(name="zone", nullable=false, insertable=true, updatable=true)
   //GETTER/SETTERS

   @Basic
   @Column(name="name", nullable=false, insertable=true, updatable=true)
   //GETTER/SETTERS

}

当我在T3Entity课程中添加此代码时,出现错误

@ManyToOne(fetch=FetchType.Lazy)
@JoinColumn(name="T2_id", referencedColumn="T2_id")
private T2Entity t2Entity;
//getters-setters

我收到了这个错误

Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [PU] failed.
Internal Exception: Exception [EclipseLink-7161] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Entity class [class com.project.entity.T3Entity] has no primary key specified. It should define either an @Id, @EmbeddedId or an @IdClass. If you have defined PK using any of these annotations then make sure that you do not have mixed access-type (both fields and properties annotated) in your entity class hierarchy.

1 个答案:

答案 0 :(得分:1)

不应该是

@JoinColumn(name="T2_id")

而不是

@JoinColumn(name="T2_id", referencedColumn="T2_id")

我不了解EclipseLink,但Hibernate甚至不允许我将不存在的列名指定为referencedColumn。如果你坚持,那应该是

@JoinColumn(name="T2_id", referencedColumn="id")

以便它引用一个存在的列。