JPA与EclipseLink ManyToMany关系无法正常工作

时间:2016-11-18 08:35:54

标签: mysql spring jpa eclipselink

我正在尝试为Phone目录设置一个应用程序,其中每个用户可以拥有多部手机,每部手机都可以分配给多个用户。我正在使用JPA和EclipseLink来完成这项工作。在检索其中一个表时,JPA抛出异常。 详情如下 -
异常追踪 -

    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 't0.users_ID' in 'where clause'
Error Code: 1054
Call: SELECT t1.ID, t1.FIRSTNAME, t1.LASTNAME, t1.TITLE, t1.CITY, t1.STATE, t1.STREET, t1.ZIP FROM Phone_User t0, User t1 WHERE ((t0.Phone_ID = ?) AND (t1.ID = t0.users_ID))
    bind => [1 parameter bound]
Query: ReadAllQuery(name="users" referenceClass=User sql="SELECT t1.ID, t1.FIRSTNAME, t1.LASTNAME, t1.TITLE, t1.CITY, t1.STATE, t1.STREET, t1.ZIP FROM Phone_User t0, User t1 WHERE ((t0.Phone_ID = ?) AND (t1.ID = t0.users_ID))") (through reference chain: edu.lab2.beans.Phone["users"]);

User.java

@Entity
@Table(name = "User")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;

private String firstname;
private String lastname;
private String title;

@Embedded
private Address address;

@ManyToMany
@JoinTable(name = "Phone_User", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "phone_id", referencedColumnName = "id"))
private List<Phone> phones;

Phone.java

@Entity
@Table(name = "Phone")
public class Phone {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String number;  // Note, phone numbers must be unique
private String description;

@Embedded
private Address address;

private List<User> users;

@ManyToMany(mappedBy="phones")
public List<User> getUsers() {
    return users;
}

表 -

    mysql> DESC User;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| id        | int(11)     | NO   | PRI | NULL    | auto_increment |
| firstname | varchar(40) | YES  |     | NULL    |                |
| lastname  | varchar(40) | YES  |     | NULL    |                |
| title     | varchar(10) | YES  |     | NULL    |                |
| street    | varchar(40) | YES  |     | NULL    |                |
| city      | varchar(40) | YES  |     | NULL    |                |
| state     | varchar(40) | YES  |     | NULL    |                |
| zip       | varchar(20) | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+
| state     | varchar(40) | YES  |     | NULL    |                |
8 rows in set (0.03 sec)

mysql> DESC Phone;
+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| id          | int(11)     | NO   | PRI | NULL    | auto_increment |
| number      | varchar(20) | YES  | UNI | NULL    |                |
| description | varchar(20) | YES  |     | NULL    |                |
| street      | varchar(40) | YES  |     | NULL    |                |
| city        | varchar(40) | YES  |     | NULL    |                |
| state       | varchar(40) | YES  |     | NULL    |                |
| zip         | varchar(20) | YES  |     | NULL    |                |
+-------------+-------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

mysql> DESC Phone_User;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| user_id  | int(11) | NO   | PRI | NULL    |       |
| phone_id | int(11) | NO   | PRI | NULL    |       |
+----------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)

我很困惑为什么EclipseLink或JPA在Join Table中搜索users_id

1 个答案:

答案 0 :(得分:1)

Phone实体混合了字段和属性访问类型(参见JPA 2.1 Specs,ch.2.3)。尝试将@ManyToMany(mappedBy="phones")注释放在private List<User> users字段上。

如果您确实需要在字段和属性(读取:getters)之间拆分注释,则可以使用@Access(AccessType.PROPERTY)注释特定属性,即在这种情况下:

@Access(AccessType.PROPERTY)
@ManyToMany(mappedBy="phones")
public List<User> getUsers() {
    return users;
}