各ユーザーが複数の電話を持ち、各電話を複数のユーザーに割り当てることができるような電話ディレクトリのようなアプリケーションを設定しようとしています。私はこの作業をするためにEclipseLinkでJPAを使用しています。そして、テーブルの1つを取り出す際に、JPAは例外をスローします。次のように 詳細は -
例外トレース -JPA with EclipseLink ManyToMany関係が動作しない
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)
JoinLinkで、EclipseLinkまたはJPAがusers_id
を検索する理由を混乱させますか?
'Phone'エンティティは、フィールドを混合して、プロパティのアクセスタイプ(JPA 2.1仕様、第2.3章を参照)。 '@ManyToMany(mappedBy =" phones ")'アノテーションを 'private list users;フィールドに配置してみてください。 –
これはうまくいった! 提案していただきありがとうございます。それは答えとして置くのに役立つかもしれません。 – pratiksanglikar