带有EmbeddedId的AssociationTable的ManyToMany本身-Spring-Data-Jpa [英] ManyToMany on itself with AssociationTable with EmbeddedId - Spring-Data-Jpa

查看:85
本文介绍了带有EmbeddedId的AssociationTable的ManyToMany本身-Spring-Data-Jpa的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ManyToMany关联期间,我在使用Hibernate时遇到了一些麻烦:/ 我想让一个用户与他的联系人. 关联表用于具有关联的creation_date及其状态(例如,活动,非活动等)

I have some trouble with Hibernate during a ManyToMany association :/ I want to have a user with his contacts. The association Table is used to have a creation_date of the association and her status (ex. Active, inactive, etc...)

  • 休眠版本:5.2.17
  • 春季靴:2.0.5

我的班级用户:

@Entity(name = "user")
public class User implements Serializable {

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "assoc_user_user", joinColumns = {@JoinColumn(name = 
"id.myself.id")}, inverseJoinColumns = {@JoinColumn(name = 
"id.contact.id")})
private List<AssocUserUser> contacts;

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "assoc_user_user", joinColumns = {@JoinColumn(name = 
"id.contact.id")}, inverseJoinColumns = {@JoinColumn(name = 
"id.myself.id")})
private List<AssocUserUser> contactOf;

}

我的关联类AssocUserUser:

@Entity(name = "assoc_user_user")
public class AssocUserUser implements Serializable {

private static final long serialVersionUID = 1L;

@EmbeddedId
private AssocUserUserId id;

@Column(name = "creation_date", nullable = false)
private LocalDateTime creationDate;

@Column(name = "status")
@Enumerated(EnumType.STRING)
private ContactStatusEnum status;

}

我的EmbeddedId类AssocUserUserId:

@Embeddable
public class AssocUserUserId implements Serializable {

private static final long serialVersionUID = 1L;

@ManyToOne
@JoinColumn(name = "user_id_myself", nullable = false)
private User myself;

@ManyToOne
@JoinColumn(name = "user_id_contact", nullable = false)
private User contact;

}

我的错误:

由于:org.hibernate.AnnotationException:外键引用 com ..... AssocUserUser来自com .....用户的号码错误 柱子.应该是2

Caused by: org.hibernate.AnnotationException: A Foreign key refering com.....AssocUserUser from com.....User has the wrong number of column. should be 2

推荐答案

好,此问题已在

Well, this question has been asked and answered at Spring-Data-JPA ManyToMany relationship with extra column but I guess it's a little different with the self reference. I'm not a big fan of all the JoinTable and JoinColumn annotations but only because they are generally redundant and, IMHO, meant for changing the defaults. My answer doesn't include those annotations so add them as needed. Note that with Spring-Data-Jpa you need repositories for each entity and so you need a repository for the Association Entity. This is different than a single entity manager in JPA. This affects a little bit how you deal with the Association Entity. Here we persist new relationships by creating and persisting new AssocUserUser entities.

我上课的方式是:

@Entity
public class User {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @OneToMany(mappedBy="myself")
    private Set<AssocUserUser> contacts;

@Entity
public class AssocUserUser {
    @EmbeddedId
    private AssocUserUserId id = new AssocUserUserId();
    @ManyToOne @MapsId("myselfId")
    private User myself;
    @ManyToOne @MapsId("contactId")
    private User contact;

@SuppressWarnings("serial")
@Embeddable
public class AssocUserUserId implements Serializable {
    private Long myselfId;
    private Long contactId;

两个实体的

Repositories

public interface UserRepository extends JpaRepository<User, Long> {
    @Query("select u from User u left outer join fetch u.contacts where u.id = :userId")
    User getContactsForUser(@Param("userId") Long userId);

public interface AssocUserUserRepository extends JpaRepository<AssocUserUser, AssocUserUserId> {

如何创建关联

private void update() {
    User user1 = new User();
    User contact1 = new User();
    userRepo.save(user1);
    userRepo.save(contact1);
    AssocUserUser assoc = new AssocUserUser();
    assoc.setMyself(user1);
    assoc.setContact(contact1);
    assocUserUserRepo.save(assoc);
}   

如何以Unidirectional方式阅读关联

private void read() {
    User me = new User();
    me.setId(1L);
    AssocUserUser assoc = new AssocUserUser();
    assoc.setMyself(me);
    List<AssocUserUser> contacts = assocUserUserRepo.findAll(Example.of(assoc));
    System.out.println(contacts.size());
}

并以Bidirectional方式阅读关联:

private void readBi() {
    User me = userRepo.getContactsForUser(1L);
    System.out.println(me.getContacts().size());
}

一如既往,请检查日志:

As always, check the logs:

create table assoc_user_user (contact_id bigint not null, myself_id bigint not null, primary key (contact_id, myself_id))
create table user (id bigint generated by default as identity, primary key (id))
alter table assoc_user_user add constraint FKaccetv956cu63fwiejjfrm0mi foreign key (contact_id) references user
alter table assoc_user_user add constraint FK1absxfuktrjnom8vwtjfqx5l0 foreign key (myself_id) references user

insert into user (id) values (null)
insert into user (id) values (null)
select assocuseru0_.contact_id as contact_1_0_0_, assocuseru0_.myself_id as myself_i2_0_0_ from assoc_user_user assocuseru0_ where assocuseru0_.contact_id=? and assocuseru0_.myself_id=?
select user0_.id as id1_1_0_ from user user0_ where user0_.id=?
select user0_.id as id1_1_0_ from user user0_ where user0_.id=?
insert into assoc_user_user (contact_id, myself_id) values (?, ?)

select assocuseru0_.contact_id as contact_1_0_, assocuseru0_.myself_id as myself_i2_0_ from assoc_user_user assocuseru0_ inner join user user1_ on assocuseru0_.myself_id=user1_.id where user1_.id=1
select user0_.id as id1_1_0_ from user user0_ where user0_.id=?
select user0_.id as id1_1_0_ from user user0_ where user0_.id=?

select user0_.id as id1_1_0_, contacts1_.contact_id as contact_1_0_1_, contacts1_.myself_id as myself_i2_0_1_, contacts1_.myself_id as myself_i2_0_0__, contacts1_.contact_id as contact_1_0_0__ from user user0_ left outer join assoc_user_user contacts1_ on user0_.id=contacts1_.myself_id where user0_.id=?
select user0_.id as id1_1_0_ from user user0_ where user0_.id=?

这篇关于带有EmbeddedId的AssociationTable的ManyToMany本身-Spring-Data-Jpa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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