JPA Bidirectionnal Uknown专栏

时间:2015-06-09 01:31:16

标签: java java-ee jpa glassfish eclipselink

我有实体,它们是单向的,所以我决定使它们双向,当它们是单向的时候一切都很好,但是当我把它们改为双向时我遇到了一些问题!!!!

我有很多实体,但我只是推荐,我在两个实体中遇到问题,一个实体OneToMany,另一个实体ManyToOne

第一个实体名为AgentOneToMany Project}:

@Entity
public class Agent implements Serializable {
    private static final long serialVersionUID = 1047335572433032863L;
    @GeneratedValue
    @Id
    private int id;
    @Basic
    private String fistname;
    @Basic
    private String lastname;
    @ManyToOne
    private Department department;
    @OneToMany(mappedBy = "agent")
    private List<Project> project;

    //Setters and getters ...
}

第二个实体名为ProjectManyToOne Agent}:

@Entity
public class Project implements Serializable {
    private static final long serialVersionUID = -6643569041998000573L;
    @GeneratedValue
    @Id
    private int id;
    @Basic
    private String crmCode;
    @Basic
    private String circuit;
    @Basic
    private String type;
    @Basic
    private String scope;
    @Basic
    @Temporal(TemporalType.DATE)
    private Date beginDate;
    @Basic
    @Temporal(TemporalType.DATE)
    private Date endDate;
    @ManyToOne
    private Agent agent;
    @ManyToOne
    private Service service;

    //Setters and getters
}

但是当我启动一些获取查询时,我得到了这个例外:

exception

javax.servlet.ServletException: javax.ejb.EJBException

root cause

javax.ejb.EJBException

root cause

javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'AGENT_ID' in 'field list'
Error Code: 1054
Call: SELECT ID, BEGINDATE, CIRCUIT, CRMCODE, ENDDATE, SCOPE, TYPE, AGENT_ID, SERVICE_ID FROM PROJECT WHERE (CRMCODE = ?)
    bind => [1 parameter bound]
Query: ReadAllQuery(referenceClass=Project sql="SELECT ID, BEGINDATE, CIRCUIT, CRMCODE, ENDDATE, SCOPE, TYPE, AGENT_ID, SERVICE_ID FROM PROJECT WHERE (CRMCODE = ?)")

root cause

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'AGENT_ID' in 'field list'
Error Code: 1054
Call: SELECT ID, BEGINDATE, CIRCUIT, CRMCODE, ENDDATE, SCOPE, TYPE, AGENT_ID, SERVICE_ID FROM PROJECT WHERE (CRMCODE = ?)
    bind => [1 parameter bound]
Query: ReadAllQuery(referenceClass=Project sql="SELECT ID, BEGINDATE, CIRCUIT, CRMCODE, ENDDATE, SCOPE, TYPE, AGENT_ID, SERVICE_ID FROM PROJECT WHERE (CRMCODE = ?)")

root cause

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'AGENT_ID' in 'field list'

在这里,我可以理解这个问题,JPA引用了数据库中不存在的AGENT_ID列,与service中的字段Project相同。

问题是为什么这个奇怪的错误?,当它创建对数据库的请求时,它不应该引用Agent.ID而不是AGENT_ID,我试图更改id中的id的名称使用Agent的{​​{1}}实体到AGENT_ID,但我在@Column时遇到了同样的问题!!!!

任何人都知道如何解决这个问题,我正在使用glassfish,感谢所有人:)

1 个答案:

答案 0 :(得分:2)

  

我用@Column(name =“AGENT_ID”)注释了代理实体的id但是我得到的问题只是JPA报告AGENT_AGENT_ID不存在

您所做的将更改Agent表中的ID列,但不会影响Project表中的连接列。

您应该执行以下操作:

class Project {
    //....

    @ManyToOne
    @JoinColumn(name="YOUR_AGENT_ID_COL_NAME");
    private Agent agent;
    //......
}