-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Problem with Many to many association
PostPosted: Mon Jan 15, 2007 1:26 am 
Newbie

Joined: Thu Oct 05, 2006 11:34 am
Posts: 8
I have two pojos Files and User with many to many association.
The code snippet of the pojos are:
Code:
@Entity
@Table(name="FILE")
public class Files implements Serializable{
private List<User> users = new ArrayList<User>();
@ManyToMany(targetEntity = User.class, cascade = { CascadeType.PERSIST,CascadeType.MERGE })
   @JoinTable(name = "FILE_USER", joinColumns = { @JoinColumn(name = "FILE_ID") }, inverseJoinColumns = { @JoinColumn(name = "USER_ID") })
   public List<User> getUsers() {
      return users;
   }
.............
}
@Entity
@Table(name = "USERS")
public class User implements Serializable{
private List<Files> files = new ArrayList<Files>();
@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST }, mappedBy = "users", targetEntity = Files.class)
   public List<Files> getFiles() {
      return files;
   }
}



I tried to save the Files object in the database using following code:
Code:
Files file = new Files();
file.setFileDescription("this is test");
file.setUploadedBy("testUser");
file.setUploadedDate(new Date());
User u=(User)getSession().load(User.class,new Long(23));//from database
Project p=(Project)getSession().load(Project.class,new Long(3));//from database
List<User> users = new ArrayList<User>();
users.add(u);
file.setUsers(users);
file.setProject(p);
getSession().saveOrUpdate(file);
getSession().flush();



I got the following error
Hibernate: insert into FILE (DESCRIPTION, PROJECT_ID, UPLOAD_DATE, UPLOAD_FILE_NAME, UPLOADED_BY) values (?, ?, ?, ?, ?)
org.hibernate.HibernateException: identifier of an instance of com.yomari.track.model.Files was altered from 6 to null

there is no insertion in the association table

_________________
.::hIBERNATe rOCKs::.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.