EclipseLink JPA:列出带有引用变量的实体
EclipseLink JPA: list entities with reference variables
我正在使用 JPA 的 eclipseLink 对我的实体执行 CRUD 操作。我面临以下问题:
我在数据库中有两个 table:
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" 子句,我会得到这个 "unknown state or association field [user1_Id] of class [com.filip.xxx.Friendship]" 错误。
具体来说:
我尝试构建此查询:
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");
它 return 是我所有友谊实体的正确列表。
注意友情实体(user1, user2)中引用变量的名称与对应的table的变量名称(user1_Id,user2_Id)不同。在我在实体中使用与 table 中相同的变量名称之前,但在持久友谊实体中收到此错误:
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 link在创建sql的时候要重命名实体的引用变量(user1 -> USER1_ID, user2 -> USER2_ID)查询,当它有问题时将其映射回实体。
我已经尝试过这些解决方案:
构建查询和 return 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。
你能帮我解决这个问题吗?
谢谢
异常
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
我正在使用 JPA 的 eclipseLink 对我的实体执行 CRUD 操作。我面临以下问题:
我在数据库中有两个 table:
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" 子句,我会得到这个 "unknown state or association field [user1_Id] of class [com.filip.xxx.Friendship]" 错误。
具体来说: 我尝试构建此查询:
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");
它 return 是我所有友谊实体的正确列表。
注意友情实体(user1, user2)中引用变量的名称与对应的table的变量名称(user1_Id,user2_Id)不同。在我在实体中使用与 table 中相同的变量名称之前,但在持久友谊实体中收到此错误:
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 link在创建sql的时候要重命名实体的引用变量(user1 -> USER1_ID, user2 -> USER2_ID)查询,当它有问题时将其映射回实体。
我已经尝试过这些解决方案: 构建查询和 return 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。
你能帮我解决这个问题吗?
谢谢
异常
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