我有manyToMany关系映射,无法正常工作。我已经阅读了许多帖子和文章,无法理解这一点。如果有人有想法,请分享。
我已尽力简化图表和代码。
我的实体看起来像这样(至少是在询问前的最后尝试): 客户
:@Entity
@Table(name = "client")
public class Client implements Serializable {
@Id
@Column(name = "client_id")
private int id;
... other fields
}
项目:
@Entity
@Table(name = "project")
public class Project implements Serializable {
@EmbeddedId
private ProjectId id;
... other fields
@Embeddable
class ProjectId implements Serializable {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "client_id", insertable = false, updatable = false)
private Client client;
@Column(name = "project_id")
private int projectId;
}
}
用户:
@Entity
@Table(name = "user")
public class User implements Serializable {
@EmbeddedId
private UserId id;
... other fields
@Embeddable
class UserId implements Serializable {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "client_id", insertable = false, updatable = false)
private Client client;
@Column(name = "user_id")
private int userId;
}
}
ProjectUser:
@Entity
@Table(name = "project_user")
public class ProjectUser implements Serializable {
@EmbeddedId
private ProjectUserId id;
... other fields
@Embeddable
class ProjectUserId implements Serializable {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "client_id", insertable = false, updatable = false)
private Client client;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "client_id", referencedColumnName = "client_id", insertable = false, updatable = false),
@JoinColumn(name = "project_id", referencedColumnName = "project_id", insertable = false, updatable = false) })
private Project project;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "client_id", referencedColumnName = "client_id", insertable = false, updatable = false),
@JoinColumn(name = "user_id", referencedColumnName = "user_id", insertable = false, updatable = false) })
private User user;
}
}
在添加ProjectUser实体之前,一切正常。
现在,当我启动服务器时,它说:
实体的映射中的重复列:ProjectUser列:client_id (应使用insert = \“ false \” update = \“ false \”映射)“}}
所以,问题是我该如何进行这项工作?
编辑: Java应用程序将主要是提供数据的REST服务。数据库设计保持原样。它具有逻辑意义,大多数业务逻辑将在数据库中。我们拥有非常丰富的DB知识的人员,因此,由于JPA / Hibernate的限制,更改数据库设计没有太大意义。
答案 0 :(得分:0)
下面的代码将让休眠状态为您的实体创建一个结构:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "client_id", insertable = false, updatable = false)
private Client client;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "project_client_id", referencedColumnName = "client_id", insertable = false, updatable = false),
@JoinColumn(name = "project_id", referencedColumnName = "project_id", insertable = false, updatable = false) })
private Project project;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "user_client_id", referencedColumnName = "client_id", insertable = false, updatable = false),
@JoinColumn(name = "user_id", referencedColumnName = "user_id", insertable = false, updatable = false) })
private User user;
但是要像您问题中的图片一样制定一个方案,请摆脱所有这些EmbeddedId
。如果您希望ProjectUser
中的项目和用户具有相同的client_id,请使用简单的id并在代码中添加验证。