2015-02-01 1 views
0

JPA의 eclipseLink를 사용하여 엔티티에서 CRUD 작업을 수행하고 있습니다. " EclipseLink JPA : 참조 변수가있는 엔티티 나열

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 ... 
} 

을 좀 기관의 목록을 검색하려는 경우에 따라 : 제가 DB에 두 개의 테이블이

: 나는 다음과 같은 문제에 직면하고있다 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)와 같지 않음에 유의하십시오. 나는 테이블로 엔티티에서 같은 변수 이름을 사용하지만, 지속 우정 개체에서이 오류를 받았다 전에 : -> USER1_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] 

는 기본적으로 나는 USER1 (일식 링크 개체의 참조 변수의 이름을 변경 이유를 이해하지 않습니다 , user2 -> USER2_ID) SQL 쿼리를 생성 할 때 엔티티로 다시 매핑하는 데 문제가 있습니다.

는 이미 이러한 솔루션을 시도 : 빌드 쿼리 및 사용자 2

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 

로 사용자 1 및 user2_Id로 user1_Id 열을 반환하지만, 위와 같은, IllegalArgumentException를 받았다.

이 문제를 해결할 수 있도록 도와 주시겠습니까? 당신은 데이터베이스 열 이름 user1_Id 이름을 사용하고 있기 때문에

감사

답변

0

예외

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

가 수신된다.

한편으로 ElementManager.createQuery() 메서드는 엔터티의 필드 이름 user1을 받아들이는 JPQL 문자열을 필요로합니다. 다음과 같이 검색어 문자열을 바꾸십시오.

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