3

I'm creating user profile in which user can enter their interests where interest is predefined list user can not select out of this list.

i have User and Interest tables with primary key userId and interestId respectively.

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

and

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

Many to Many mapping table

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

here is my code

User.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 -----

so when i run this it insert user in User table selected interests in Interest table and mapping in UserInterest Table

everything is ok but i don't need to insert interest in interest table because i already insert required interests.

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 (?, ?)

I don't want to execute

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

queries.

I search lots of for this problem but i can't find any solution.

4

2 回答 2

3

问题不在于休眠,而是您的代码..如果您正在这样做new Interest(..),休眠会将它们保存在数据库中,您应该执行以下操作:

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

    public String execute() {

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

    }

}
于 2013-10-04T19:36:57.230 回答
-1

首先,您必须确保您有 Getter 和 Setter for interestId ,如果您错过了“IDE 不显示警告”,则必须添加它们。

那么你必须这样做

@Entity
class user{
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE})
@JoinTable(name="user_interest",joinColumns= {@JoinColumn(name="user_id", referencedColumnName = "id")},inverseJoinColumns= {@JoinColumn(name="interest_id", referencedColumnName = "id")})
private Set<Interest> interests= new HashSet<>();
 .
 .
 }
@Entity
@JsonIgnoreProperties({"users"})
class interest{
@ManyToMany(mappedBy="interests")
private List<User> users;
}
于 2017-10-04T06:51:34.607 回答