gpt4 book ai didi

mysql - JPA 与 EclipseLink ManyToMany 关系不起作用

转载 作者:行者123 更新时间:2023-11-29 11:10:25 25 4
gpt4 key购买 nike

我正在尝试设置一个类似电话目录的应用程序,其中每个用户可以拥有多个电话,并且每个电话可以分配给多个用户。我使用 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"]);

用户.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;

电话.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 在连接表中搜索 users_id

最佳答案

Phone实体混合了字段和属性访问类型(请参阅 JPA 2.1 规范,第 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;
}

关于mysql - JPA 与 EclipseLink ManyToMany 关系不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40672546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com