gpt4 book ai didi

java - 如何防止hibernate在多对多映射中插入一个表

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:27:51 24 4
gpt4 key购买 nike

我正在创建用户配置文件,用户可以在其中输入他们的兴趣,其中兴趣是预定义的列表,用户无法从该列表中选择。

我有 User 和 Interest 表,主键分别为 userIdinterestId

             Table:User
-------------------------------------
userId userName
1 aaaa
2 bbbb
3 cccc

           Table:Intrest
-------------------------------------
interestId interestName
1 Sports
2 Reading
3 Music

多对多映射表

         Table:UserIntrest
-------------------------------------
userId interestId
1 1
1 2
1 3

这是我的代码

用户.java

@Entity
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long userId;
private String username;
private String password;

@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(
name="UserIntrest",
joinColumns={ @JoinColumn(name="userId",referencedColumnName="userId") },
inverseJoinColumns={ @JoinColumn(name="intrestId", referencedColumnName="intrestId") }
)
private List<Intrest> intrestList;

----- Getters Setters -----

Intrest.java

@Entity
@Table(name="intrest")
public class Intrest {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int intrestId;
private String intrestName;

public Intrest(String name){
this.intrestName = name;
}
----- Getters & Setters -----

ActionClass.java

public class ActionClass extends ActionSupport {


private User user;
private List<String> intrestList;//for get list of intrest from jsp select tag
private List<Intrest> intrestLst = new ArrayList<Intrest>();

public String execute(){
for(int i = 0;i<intrestList.size();i++){
System.out.print(intrestLst.add(new Intrest(intrestList.get(i))));
}
user.setIntrestList(intrestLst);

SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.getCurrentSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();


return SUCCESS;
}

----- Getters & Setters -----

所以当我运行它时,它会在用户表中插入用户,在兴趣表中选择兴趣并在用户兴趣表中映射

一切正常,但我不需要在兴趣表中插入兴趣,因为我已经插入了所需的兴趣。

Hibernate: insert into User (password, username) values (?, ?)
Hibernate: insert into intrest (intrestName) values (?)
Hibernate: insert into intrest (intrestName) values (?)
Hibernate: insert into intrest (intrestName) values (?)
Hibernate: insert into intrest (intrestName) values (?)
Hibernate: insert into UserIntrest (userId, intrestId) values (?, ?)
Hibernate: insert into UserIntrest (userId, intrestId) values (?, ?)
Hibernate: insert into UserIntrest (userId, intrestId) values (?, ?)
Hibernate: insert into UserIntrest (userId, intrestId) values (?, ?)

我不想执行

Hibernate: insert into intrest (intrestName) values (?)
Hibernate: insert into intrest (intrestName) values (?)
Hibernate: insert into intrest (intrestName) values (?)
Hibernate: insert into intrest (intrestName) values (?)

查询。

我为这个问题搜索了很多,但找不到任何解决方案。

最佳答案

问题不在 hibernate 中,而是你的代码..如果你正在做new Interest(..), hibernate 会将它们持久化在数据库中,你应该这样做:

public class MyAction extends ActionSupport {
private Long[] interestIds;

public String execute() {

// get interests with ids
// set interests to user
// save user

}

}

关于java - 如何防止hibernate在多对多映射中插入一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19188751/

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