如何在冬眠中建立友谊关系? [英] How to model a friendship relationship in hibernate?

查看:139
本文介绍了如何在冬眠中建立友谊关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要建立友谊关系.我有一个带有两个主键的友谊班,每个主键都是会员类.我收到以下异常:

I need to have a friendship relationship. I have a friendship class with two primary keys that each is a Member class. I am receiving following exception:

org.hibernate.MappingException: Foreign key (FK_8ynretl1yt1xe3gcvfytrvpq:Friendship [])) must have same number of columns as the referenced primary key (Member [username])

友谊

@Entity
public class Friendship implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = -1234656876554786549L;
    @Id
    @ManyToOne
    Member requester;
    @Id
    @ManyToOne
    Member friend;
    @Temporal(javax.persistence.TemporalType.DATE)
Date date;

会员

@Entity
public class Member {
    @Id
    @MapsId
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "username")
    Credential credential;
    @Column(nullable = false)
    String fname;
    @Column(nullable = false)
    String lname;
    @Column(nullable = false)
    short gender;

凭据

@Entity
public class Credential {
    @Id
    @Column(nullable = false, unique = true)
    private String username;
    @Column(nullable = false)
    private String password;
    @Column(nullable = false)
    private String authority;
    @Column(nullable = false)
    private boolean enabled;

推荐答案

如果使用多个没有标识符类型的id属性,则MemberCredential应该实现Serializable,您的映射是好的,这似乎成为Hibernate中的错误.

Putting aside that Member and Credential should implement Serializable if multiple id properties without identifier type are used, your mappings are good, and this seems to be a bug in Hibernate.

解决方案1 ​​

我通过在friend中的referencedColumnNameFriendship中的requester关联中声明了这一点来设法完成这项工作:

I managed to make this work by declaring referencedColumnName in friend and requester associations in Friendship:

@Id
@ManyToOne
@JoinColumn(referencedColumnName = "username")
Member requester;

@Id
@ManyToOne
@JoinColumn(referencedColumnName = "username")
Member friend;

这样,我们可以明确地告诉Hibernate复合ID引用哪些列,这样就不必自己弄清楚它了.

This way we explicitly tell Hibernate which columns the composite id references, so that it does not have to figure it out itself.

解决方案2

解决方案1让我想到了可能是Hibernate中的错误的原因.似乎它在某种程度上受Hibernate处理实体映射的顺序的影响.如果您显式声明了被引用的列,则一切正常,否则,Hibernate似乎在构建复合键时并不了解有关被引用列的所有详细信息.

The solution 1 made me think of what could be the cause of the bug in Hibernate. It seems that it is somehow affected by the order in which Hibernate processes the entity mappings. If you explicitly declare the referenced column, everything works fine, otherwise it seems that Hibernate does not know all the details about the referenced column at the time it builds the composite key.

因此,我将为会话工厂配置添加带注释的类的顺序更改为:

So I changed the order in which I add annotated classes to the session factory configuration to:

Credential
Member
Friendship

,然后一切都可以使用原始映射(在MemberCredential中实现Serializable之后).

and then everything worked with your original mappings (after implementing Serializable in Member and Credential).

我以编程方式将这些类按顺序添加到Configuration类中,但是我假设通过在persistence.xmlhibernate.cfg.xml中指定此顺序可以实现相同的效果:

I added the classes in this order programmatically to the Configuration class, but I assume the same effect could be achieved by specifying this order in the persistence.xml or hibernate.cfg.xml:

<class>Credential</class>
<class>Member</class>
<class>Friendship</class>

尽管如此,此解决方案仅用于演示目的(您或其他人以后可以在不考虑此问题的情况下重新排序类),因此我建议使用解决方案1.

Nevertheless, this solution is just for demonstrative purposes (you or someone else can later reorder the classes without keeping this issue in mind), so I suggest using solution 1.

注意

您会更好地了解用例,但根据我个人的观点,您应该使用@IdClass@EmbeddedId,因为它们在JPA中已标准化;没有标识符类型的多个id属性是Hibernate的特定功能.除了能够更轻松地构造用于搜索和查询相应实体的主键对象外,专用的PK对象通常更轻巧,并且在序列化时可以提供更好的性能,尤其是在启用二级缓存的情况下.

You know your use cases better, but in my personal opinion you should use @IdClass or @EmbeddedId since they are standardized in JPA; multiple id properties without identifier type is a Hibernate specific feature. Besides being able to easier construct the primary key object by which you will search and query the corresponding entities, a dedicated PK object is usually much lighter and offers better performance when serialized, especially if second level cache is enabled.

这篇关于如何在冬眠中建立友谊关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆