Author | Message |
---|---|
abhishek_sharma
Posts: 79
|
Posted 12:01 Aug 28, 2012 |
I have this scenario, if anyone can spot where I am doing wrong
User Class Id Name @OneToMany(mappedBy="user") AlbumRelation albumRelation
Album Id Name @ManyToMany(mappedBy="album") AlbumRelation albumRelation
AlbumRelation Id @ManyToOne @JoinColumn(name = "user_id") User user @ManyToMany @JoinColumn(name = "album_id") Album album Getting this error
SEVERE: Servlet.service() for servlet [fpsimages] in context with path [/fpsimages] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: org.hibernate.type.SerializationException: could not deserialize; nested exception is javax.persistence.PersistenceException: org.hibernate.type.SerializationException: could not deserialize] with root cause |
dely
Posts: 32
|
Posted 12:55 Aug 28, 2012 |
Isn't OneToMany and ManyToMany used for annotating collections? Also, what is the point of AlbumRelation? If you are using it for mapping users to albums, then you are doing it wrong. Think in terms of objects. |
abhishek_sharma
Posts: 79
|
Posted 13:03 Aug 28, 2012 |
My mistake; type
One User can access multiple Album, so I need AlbumRelation Table
User Id Name @OneToMany(mappedBy = "user") Set<AlbumRelation> albumRelation
Album id Name @ManyToMany(mappedBy = "album") Set<AlbumRelation> albumRelataion // I don't need this , but just to get all users who can access that album
AlbumRelation Id @ManyToOne @JoinColumn(name = "user_id") User user @ManyToMany @JoinColumn(name="album_id") Album album |
dely
Posts: 32
|
Posted 08:22 Aug 29, 2012 |
It might help to attach the class rather than giving snippets of it since the problem may lie elsewhere in the class. Also, AlbumRelation still has a @ManyToMany that annotates a non-collection type. And I still don't see the use of AlbumRelation. If a User can access many Albums, and an Album can have many Users, then thats a ManyToMany relation between Album and User. |
abhishek_sharma
Posts: 79
|
Posted 09:39 Aug 29, 2012 |
Thanks Dely for ur reply, I got it work. I needed AlbumRelation because I have a field write_permission too , which is true if user write (upload image) permission to an album So a multiple user can have multiple album in their list but not all album comes with write permission So I wanted something like user.getAssignAlbum().get(0).getWritePermission() I did OneToMany from User to AlbumRelation and ManyToOne vice-versa. Find attachment and lemme know I can do something better
Thanks |
abhishek_sharma
Posts: 79
|
Posted 12:00 Sep 20, 2012 |
Need a little help with @ManyToOne mapping I have an Assignment Class (model)
@ManyToOne(targetEntity=User.class, cascade= CascadeType.PERSIST)
@ManyToOne(targetEntity=Company.class,cascade= CascadeType.PERSIST)
When I do save everything insert into DB except User and Company .
|
dely
Posts: 32
|
Posted 14:40 Sep 20, 2012 |
How are you saving the entity? Are you actually doing a persist or a merge? Does CascadeType.ALL work? Are you using JPA, and if so, are the annotations imported JPA or did you accidentally imported Hibernate annotations? |
abhishek_sharma
Posts: 79
|
Posted 15:12 Sep 20, 2012 |
I am saving Assignment entity as merge No CascaseType.All not working for saving data rather when I am deleting an assignment it also deleting user and company associated with it. I don't want to delete company and user. I am using JPA , I accidently imported Hibernate annotations My goal is when I create an assignment I have a dropdown of User and Company and I can choose from that dropdown . Worst part I am not getting any error Lemme know if u need further info and ya thnx for ur help |
dely
Posts: 32
|
Posted 16:04 Sep 20, 2012 |
Hmm that is odd. How is your User class and Company class annotated. Is this a unidirectional or bidirectional? If it is bidirectional, did you make sure to set the reference objects on both sides? Also, set your CascadeType to PERSIST and MERGE:
@ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name="user_id")
private User user;
@ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name="company_id")
private Company company;
|
abhishek_sharma
Posts: 79
|
Posted 16:27 Sep 20, 2012 |
Its uni directional . I just want to add user and company to assignment. using Persist was also deleting user and or company on delete of assignment
|