【问题标题】:EclipseLink JPA: list entities with reference variablesEclipseLink JPA:列出具有引用变量的实体
【发布时间】:2015-02-01 13:36:07
【问题描述】:

我正在使用 JPA 的 eclipseLink 对我的实体执行 CRUD 操作。我面临以下问题:

我在数据库中有两个表:

CREATE TABLE User (
   id INTEGER PRIMARY KEY AUTO_INCREMENT,
   username VARCHAR(30) NOT NULL UNIQUE,
   email VARCHAR(50) NOT NULL UNIQUE,
   password VARCHAR(255) NOT NULL,
   signUpDate timestamp NOT NULL DEFAULT NOW()
);

CREATE TABLE Friendship (
   id INTEGER PRIMARY KEY AUTO_INCREMENT,
   friendsSince timestamp NOT NULL DEFAULT NOW(),
   user1_Id INTEGER NOT NULL REFERENCES User(id),
   user2_Id INTEGER NOT NULL REFERENCES User(id)
);

对应的实体

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String username;
    private String email;
    private String password;
    @Temporal(value = TemporalType.DATE)
    private Date signUpDate;
    // constructors & setters & getters ...
}

@Entity
public class Friendship {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @ManyToOne
    @JoinColumn(name="user1_Id", referencedColumnName = "id")
    private User user1;
    @ManyToOne
    @JoinColumn(name="user2_Id", referencedColumnName = "id")
    private User user2;
    @Temporal(value = TemporalType.DATE)
    private Date friendsSince;

    // constructors & setters & getters ... 
}

如果我想检索一些实体的列表,根据查询的“WHERE”子句,我会收到“类 [com.filip.xxx.Friendship] 的未知状态或关联字段 [user1_Id]”错误。

具体来说: 我尝试构建这个查询:

Query query = mgr.createQuery("select f.id ,f.friendsSince, f.user1_Id, f.user2_Id from Friendship f where f.user1_Id = :user1Id and f.user2_Id = :user2Id or f.user1_Id = :user11Id and f.user2_Id = :user12Id");

并收到此异常:

java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Error compiling the query [select f.id ,f.friendsSince, f.user1_Id, f.user2_Id from Friendship f where f.user1_Id = :user1Id and f.user2_Id = :user2Id or f.user1_Id = :user11Id and f.user2_Id = :user12Id], line 1, column 31: unknown state or association field [user1_Id] of class [com.filip.xxx.Friendship].

似乎将属性映射回实体有问题,因为我在持久化这两个实体方面没有问题。

有趣的是,如果我运行这个查询:

Query query = mgr.createQuery("select f from Friendship f");

它返回所有友谊实体的正确列表。

注意友情实体中的引用变量名(user1, user2)与对应表的变量名(user1_Id, user2_Id)不同。在我在实体中使用与表中相同的变量名称之前,但在持久友谊实体中收到此错误:

javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461):
org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'USER1_ID' in 'field list'
Error Code: 1054
Call: INSERT INTO FRIENDSHIP (FRIENDSSINCE, USER1_ID, USER2_ID) VALUES (?, ?, ?)
bind => [3 parameters bound]

基本上我不明白,为什么 eclipse 链接在创建 sql 查询时重命名实体的引用变量(user1 -> USER1_ID,user2 -> USER2_ID),而将其映射回实体时遇到问题。

我已经尝试过以下解决方案: 构建查询并返回 user1_Id 列作为 user1 和 user2_Id 作为 user2

select f.id ,f.friendsSince, f.user1_Id as user1, f.user2_Id as user2 from Friendship f where f.user1_Id = :user1Id and f.user2_Id = :user2Id or f.user1_Id = :user11Id and f.user2_Id = :user12Id

但收到与上述相同的 IllegalArgumentException。

你能帮我解决这个问题吗?

谢谢

【问题讨论】:

    标签: java jpa eclipselink entitymanager


    【解决方案1】:

    例外

    unknown state or association field [user1_Id] of class [com.filip.xxx.Friendship]

    收到是因为您使用的是 user1_Id 名称,这是一个数据库列名称。

    另一方面,ElementManager.createQuery() 方法需要 JPQL 字符串,该字符串接受实体的字段名称 user1。尝试将您的查询字符串替换为:

    select f.id, f.friendsSince, f.user1, f.user2
    from Friendship f 
    where f.user1 = :user1Id and 
          f.user2 = :user2Id or 
          f.user1 = :user11Id and 
          f.user2 = :user12Id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 2011-09-25
      相关资源
      最近更新 更多