JPA新手......
我在MySQL 5数据库中有以下预先存在的表:
这些关系被映射到App和User之间的User_App_Bridge表(顾名思义,桥梁)作为一对多基数。
E-R图:
用户------ User_App_Bridge ------ App
1用户与User_App_Bridge表中的许多应用相关联。
1 App与User_App_Bridge表中的许多用户相关联。
User_App_Bridge表的DDL:
CREATE TABLE `User_App_Bridge` (
`User_App_Bridge_Id` int(11) NOT NULL AUTO_INCREMENT,
`User_Id` int(11) NOT NULL,
`App_Id` int(11) NOT NULL,
PRIMARY KEY (`User_App_Bridge_Id`),
KEY `App_Id` (`App_Id`),
KEY `User_Id` (`User_Id`),
CONSTRAINT `user_app_bridge_ibfk_1` FOREIGN KEY (`App_Id`) REFERENCES `App` (`App_Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
假设我有以下JPA带注释的类,它们映射到这些表:
@Entity
public class App {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "App_Id")
private long appId;
@OneToMany
@JoinTable
(
name = "UserAppBridge",
joinColumns = { @JoinColumn(name="App_Id", referencedColumnName = "App_Id") },
inverseJoinColumns = { @JoinColumn(name="User_Id", referencedColumnName = "User_Id") }
)
private List<User> users;
@Column(name = "App_Name")
private String appName;
// Getters & Setter methods
}
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "User_Id")
private long userId;
@OneToMany
@JoinTable
(
name = "UserAppBridge",
joinColumns = { @JoinColumn(name="User_Id", referencedColumnName = "User_Id") },
inverseJoinColumns = { @JoinColumn(name="App_Id", referencedColumnName = "App_Id", unique = true) }
)
private List<App> apps;
@Column(name = "User_Name")
private String userName;
// Getters & Setter methods
}
@Entity
public class UserAppBridge {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "User_App_Bridge_Id")
private long userAppBridgeId;
@Column(name = "User_Id")
private long userId;
@Column(name = "App_Id")
private long appId;
// Getters & Setter methods
}
问题(S):
这是(@JoinTable中的块)使用UserAppBridge进行用户和应用程序的一对多映射的正确方法吗?
在@JoinTable内应该是列&amp; referencedColumnName被分配给SQL值(例如name =“User_Id”,referencedColumnName =“User_Id”) 或者它应该是Java引用名称(例如name =“userId”,referencedColumnName =“userId”)?
在inverseJoinColumns代码中,黑色是唯一的= true必需的(它是什么用的)?
我是否需要在UserAppBridge类中执行任何其他操作(用于连接到应用和用户)?
感谢您抽出宝贵时间阅读此内容......
答案 0 :(得分:4)
您的问题的答案:
1.不,不是。您需要在User和UserAppBridge之间创建oneToMany映射,并在App和UserAppBridge之间创建oneToMany映射。这是代码:
在用户实体中:
@OneToMany(mappedBy = "user")
private Set<UserAppBridge> userAppBridgeSet;
在App实体中:
@OneToMany(mappedBy = "app")
private Set<UserAppBridge> userAppBridgeSet;
在UserAppBridge实体中:
@Entity
@IdClass(UserAppBridgeId.class)
public class UserAppBridge{
@Id
@Column(name = "User_App_Bridge_Id")
private long userAppBridgeId;
@Id
@Column(name = "User_Id")
private long userId;
@Id
@Column(name = "App_Id")
private long appId;
@ManyToOne
@PrimaryKeyJoinColumn(name="User_Id", referencedColumnName="User_Id")
private User user;
@ManyToOne
@PrimaryKeyJoinColumn(name="App_Id", referencedColumnName="App_Id")
private App app;
// Getters & Setter methods
}
在UserAppBridgeId类中:
public class UserAppBridgeId{
private long userAppBridgeId;
private int userId;
private int appId;
// Getters & Setter methods
}
有关如何创建高级manyToMany关系的详细信息,请参阅wiki link
它应该是一个SQL名称
使用&#34; unique = true&#34;你正在强迫一个单一的关系,没有&#34; unique = true&#34;它会很多很多。
说明: 让我们有一段代码:
@OneToMany
@JoinTable{
//Here should be the table name and not the entity name
name = "User_App_Bridge",
joinColumns = { @JoinColumn(name="User_Id", referencedColumnName = "User_Id") },
inverseJoinColumns = { @JoinColumn(name="App_Id", referencedColumnName = "App_Id", unique = true) }
}
private List<App> apps;
如果有&#34; unique = true&#34;属性,你的映射表只能有唯一的App_id(相同的App_id在映射表中不能再出现一次),而它可以有任何User_id(相同的User_id可以在映射表中出现一次以上)。这意味着,一个User_id可能已经分配了更多的App_id,而单个App_id可能只分配了一个User_id。因此,你强制一个oneToMany关系。