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.