gpt4 book ai didi

java - 用于从 ManyToMany 连接表检索行的 HQL 查询

转载 作者:行者123 更新时间:2023-12-01 13:05:08 25 4
gpt4 key购买 nike

我正在开发一个网站,用户可以订阅组织。当我要实现订阅功能时,我遇到以下问题。

在排序中,我想创建 ManyToMany 连接表的模型类,用于从表中检索行以检查用户订阅了哪些组织。在 Hibernate 中,我无法创建没有主键的表。但是在连接表中,一个用户可以订阅多个组织,而一个组织有多个订阅者,因此主键是重复的,我得到了异常 ERROR: Duplicate entry '1' for key 'PRIMARY' .

hibernate.cfg.xml包含

<mapping class="model.User"/>
<mapping class="model.Post"/>
<mapping class="model.UserSubscribes"/>

User.java

package model;

@Entity
@Table(name="user",
uniqueConstraints = {@UniqueConstraint(columnNames={"email"})}
)
@org.hibernate.annotations.Entity(dynamicUpdate=true,selectBeforeUpdate=true)

public class User implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long userId;//1
private String email;//1
private String password;//

public User(long userId, String email, String password){
this.userId = userId;
this.email = email;
this.password = password;
}

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name="UserSubscribes",
joinColumns={ @JoinColumn(name="userId",referencedColumnName="userId") },
inverseJoinColumns={ @JoinColumn(name="orgId", referencedColumnName="orgId") }
)
private Collection<Organisation> orgSubscribes = new ArrayList<Organisation>();


//Getter & Setter

}

组织.java

package model;

@Entity
@Table(name="org",
uniqueConstraints = {@UniqueConstraint(columnNames={"email"})}
)
@org.hibernate.annotations.Entity(dynamicUpdate=true,selectBeforeUpdate=true)

public class Organisation implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long orgId;
private String email;
private String password;

public Organisation(long orgId, String email, String password){
this.orgId = orgId;
this.email = email;
this.password = password;
}

//Getter & Setter
}

UserSubscribes.java

package model;

@Entity
@Table(name="UserSubscribes")
public class UserSubscribes implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long userId;
private long orgId;

//Getter & Setter
}

订阅.java

package view.action;

public class Subscribe extends ActionSupport {

public String execute(){

Session session = HibernateUtill.getSessionFactory().getCurrentSession();
session.beginTransaction();

System.out.println("Subscribbbbbbbbbbbbbbbbbbbbbbbbbbbbb");

User u1 = new User(1, "ppp", "ppp");
User u2 = new User(2, "qqq", "qqq");
Organisation o1 = new Organisation(1, "ppp", "ppp");
Organisation o2 = new Organisation(2, "qqq", "qqq");
Organisation o3 = new Organisation(3, "www", "www");
Organisation o4 = new Organisation(4, "eee", "eee");

session.save(o1);
session.save(o2);
session.save(o3);
session.save(o4);
session.save(u1);
session.save(u2);

u1.getOrgSubscribes().add(o1);
u1.getOrgSubscribes().add(o2);
u1.getOrgSubscribes().add(o3);

session.saveOrUpdate(u1);

session.getTransaction().commit();

return SUCCESS;
}
}

我得到了这个输出和错误

Subscribbbbbbbbbbbbbbbbbbbbbbbbbbbbb
Hibernate: insert into org (email, password) values (?, ?)
Hibernate: insert into org (email, password) values (?, ?)
Hibernate: insert into org (email, password) values (?, ?)
Hibernate: insert into org (email, password) values (?, ?)
Hibernate: insert into user (email, password) values (?, ?)
Hibernate: insert into user (email, password) values (?, ?)
Hibernate: insert into UserSubscribes (userId, orgId) values (?, ?)
Hibernate: insert into UserSubscribes (userId, orgId) values (?, ?)
Apr 27, 2014 4:43:52 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1062, SQLState: 23000
Apr 27, 2014 4:43:52 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Duplicate entry '1' for key 'PRIMARY'

如果我删除 <mapping class="model.UserSubscribes"/>从 hibernate.cfg.xml 映射,它可以完美地工作,如以下输出。

Subscribbbbbbbbbbbbbbbbbbbbbbbbbbbbb
Hibernate: insert into org (email, password) values (?, ?)
Hibernate: insert into org (email, password) values (?, ?)
Hibernate: insert into org (email, password) values (?, ?)
Hibernate: insert into org (email, password) values (?, ?)
Hibernate: insert into user (email, password) values (?, ?)
Hibernate: insert into user (email, password) values (?, ?)
Hibernate: insert into UserSubscribes (userId, orgId) values (?, ?)
Hibernate: insert into UserSubscribes (userId, orgId) values (?, ?)
Hibernate: insert into UserSubscribes (userId, orgId) values (?, ?)

输出是

enter image description here

但如果不在 hibernate.cfg.xml 文件中映射此表,我无法从中检索行(使用 HQL)。
如果这个问题有任何可能的解决方案,我真的很感谢你。预先感谢您。

最佳答案

连接表不应映射为实体。您只需要用户、组织以及这两个实体之间的多对多关联。

In sort I want to create model class of ManyToMany join table for retrieve rows from table to check which Organisations are subscribe by user

这可以通过协会来完成:

User user = em.find(User.class, userId);
Set<Organization> organizations = user.getOrganizations();

或者使用简单的 JPQL 查询:

select o from User u inner join u.organizations o where u.id = :userId

关于java - 用于从 ManyToMany 连接表检索行的 HQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23322919/

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