gpt4 book ai didi

java - JPA 2 Hibernate 映射与主键中的复合键使用@IdClass 与 3 层结构

转载 作者:搜寻专家 更新时间:2023-11-01 03:37:37 25 4
gpt4 key购买 nike

这个问题非常类似于:JPA (Hibernate, EclipseLink) mapping: why doesn't this code work (chain of 2 relationships using JPA 2.0, @EmbeddedId composite PK-FK)?

实际上我唯一的(从我发现的意义来看)区别是我使用 @IdClass 并且我很可能无法切换到与 hibernate 不同的提供程序。

但无论如何这里是代码(删除了不重要的部分):

PermissionContextType.java:

@Entity
@IdClass(PermissionContextTypePk.class)
public class PermissionContextType{
@Id
private String id;

@Id
@JoinColumn (name = "PROJECT", referencedColumnName = "ID")
@ManyToOne ()
private Project project;

public static class PermissionContextTypePk implements Serializable{
public String project;
public String id;
// ... eq and hashCode here ...
}

}

PermissionContext.java:

@Entity
@IdClass(PermissionContextPk.class)
public class PermissionContext{
@Id
private String id;

@Id
@JoinColumns ({
@JoinColumn (name = "PROJECT", referencedColumnName = "PROJECT"),
@JoinColumn (name = "PERMISSIONCONTEXTTYPE", referencedColumnName = "ID")
})
@ManyToOne
private PermissionContextType permissionContextType;

public static class PermissionContextPk implements Serializable{
public String id;
public PermissionContextTypePk permissionContextType;
// ... eq and hashCode here ...
}
}

Permission.java:

@Entity
@IdClass(PermissionPk.class)
public class Permission{

@Id
private String id;

@Id
@JoinColumns ({
@JoinColumn (name = "PROJECT", referencedColumnName = "PROJECT"),
@JoinColumn (name = "PERMISSIONCONTEXTTYPE", referencedColumnName = "PERMISSIONCONTEXTTYPE"),
@JoinColumn (name = "PERMISSIONCONTEXT", referencedColumnName = "ID")
})
@ManyToOne
private PermissionContext permissionContext;

public static class PermissionPk implements Serializable{
public String id;
public PermissionContextPk permissionContext;
// ... eq and hashCode here ...
}
}

我得到的是:

 org.hibernate.AssertionFailure: Unexpected nested component on the referenced entity when mapping a @MapsId: PermissionContext 
Caused by: org.hibernate.AssertionFailure: org.hibernate.AssertionFailure: Unexpected nested component on the referenced entity when mapping a @MapsId: PermissionContext

有谁知道这是否是一个 hibernate 错误,我应该将其发布到他们的问题跟踪系统上(并祈祷我能够更新到给定的 hibernate 版本)或者我绑定(bind)实体的方式是否存在根本性错误?

我已经用 EAP 6.1 (4.2.0) 和 wildfly 上的 hibernate 实现检查了它(真的不知道是哪一个。)

最佳答案

好的,这就是我目前所发现的:

谢谢我的 friend :https://hibernate.atlassian.net/browse/HHH-5764这很可能是造成这种行为的原因。

我找到了解决方法:

权限.java:

@Entity
@IdClass(PermissionPk.class)
public class Permission{

@Id
private String id;

// for the next 3 fields there are no public acessors, so the public API of the class was not changed !
@Id
@Column(name = "PROJECT")
private String projectId;

@Id
@Column(name = "PERMISSIONCONTEXTTYPE")
private String permissionContextTypeId;

@Id
@Column(name = "PERMISSIONCONTEXT")
private String permissionContextId;


@JoinColumns ({
@JoinColumn (name = "PROJECT", referencedColumnName = "PROJECT", updatable = false, insertable = false),
@JoinColumn (name = "PERMISSIONCONTEXTTYPE", referencedColumnName = "PERMISSIONCONTEXTTYPE", updatable = false, insertable = false),
@JoinColumn (name = "PERMISSIONCONTEXT", referencedColumnName = "ID", updatable = false, insertable = false)
})
@ManyToOne
private PermissionContext permissionContext;

public static class PermissionPk implements Serializable{
// previously they where private as well, but removed public constructor for the sake of simplicity of the question - so no changes where necesary in public API of the class !
private String id;
private String projectId;
private String permissionContextTypeId;
private String permissionContextId;

public PermissionPk () {}

public PermissionPk (String aId, PermissionContextPk aPermissionContext) {
this.id = aId;
permissionContextId = aPermissionContext.id;
permissionContextTypeId = aPermissionContext.permissionContextType.id;
projectId = aPermissionContext.permissionContextType.project;
}
... eq and hashCode here ...
}
}

这个变通办法的好处是它不会以任何方式改变类的公共(public) API(唯一的变化是我需要使 Pk 的上下文和上下文类型中的字段对 PermissionPk 可见 - 它们之前是私有(private)的,只有一个公共(public)构造函数 [但再次简化了问题]),也没有改变 jpql 查询,并且同时,变通办法是可扩展的(可扩展到任何层数 - 只要每个偶数 pk 不包含另一个 pk),因此如果错误得到解决,将很容易删除变通办法。

我仍然很乐意接受任何关于我的解决方法或问题本身的评论。

关于java - JPA 2 Hibernate 映射与主键中的复合键使用@IdClass 与 3 层结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25874399/

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