【发布时间】: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