-->
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: [JPA/Hibernate] @IdClass force to persist associated object
PostPosted: Sun May 20, 2012 8:05 am 
Newbie

Joined: Sat May 19, 2012 7:42 am
Posts: 4
Hi,

I woud like to know why my associated classes are forced to be persisted when I use @IdClass ?

My classes :
Code:
@Entity
@IdClass(ScoreId.class)
public class Score implements Serializable {
...
   @Id
   @ManyToOne(cascade = {})
   @JoinColumn(name = "id_ticket", insertable = false, updatable = false)
   @Cascade({}) // I don't want any cascade !
   @NotNull
   public Ticket getTicket() {
      return ticket;
   }

   @Id
   @ManyToOne(cascade = {})
   @Cascade({})
   @JoinColumn(name = "id_user", insertable = false, updatable = false)
   @NotNull
   public User getUser() {
      return user;
   }

   public int getScore() {
      return score;
   }
}

Code:
public class ScoreId implements Serializable {
   private Integer ticket;
   private Integer user;
// ... getter, setter, equals and hashcode ...

Code:
@Entity
public class Ticket implements Serializable {
   @Id
   @GeneratedValue
   @Column(name = "id_ticket")
   public Integer getIdTicket() {
      return idTicket;
   }
}

Code:
@Entity
public class User implements Serializable {
// ...
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "id_user")
   public Integer getUserId() {
      return userId;
   }


When I'm saving Score object, my Ticket and User are persisted. I don't specified any cascade, so why ?

My test code :
Code:
@Test
   public void testSave() {
      User user = userDAO.findById(2);
      assertNotNull(user);
      Ticket ticket = ticketDAO.findById(6);
      assertNotNull(ticket);

      Score score = new Score();
      score.setScore(3);
      score.setTicket(ticket);
      score.setUser(user);

      scoreDAO.save(score); // using hibernate.Session.saveOrUpdate
   }


Thanks,
(and sorry for my english...)

Dush


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.