-->
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.  [ 2 posts ] 
Author Message
 Post subject: sequence used to persist objects with one-to-many assocition
PostPosted: Fri May 05, 2006 7:59 pm 
Newbie

Joined: Tue Jul 20, 2004 1:25 pm
Posts: 12
Hibernate version: 3.2 cr1
Hibernate annotations version: 3.1.0.Beta10b

When I attempt to persist my ParentOneToMany class (annotated below) I am seeing my defined sequence being ignored in favor of a SequenceHiLoGenerator value. From the logs I see the sequence being hit and a correct value returned, but it isn't being used. If I switch to using xml mapped classes the sequence is used as expected.

Any thoughts on why the sequence is being ignored when I use annotations?


Annotated Classes
Code:
@Entity
@Table(name="TblParentOneToMany")
@SequenceGenerator(name="ParentSeq", sequenceName="PARENT_SEQ")
public class ParentOneToMany
{
   private long _id;
   private String _name;
   private Set<ChildOneToMany> _children;
   
   @Id
   @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ParentSeq")
   public long getId()
   {
      return _id;
   }
   
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy="parent")
   @Cascade(value = {org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
   public Set<ChildOneToMany> getChildren()
   {
      return _children;
   }
   ... Other getter/setters removed for brevity ...
}

@Entity
@Table(name="TblChildOneToMany")
@SequenceGenerator(name="ChildSeq", sequenceName="CHILD_SEQ")
public class ChildOneToMany
{
   private long _id;
   private ParentOneToMany _parent;
   private String _name;
   
   @Id
   @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ChildSeq")
   public long getId()
   {
      return _id;
   }
   @ManyToOne
   @JoinColumn(name="parent_id")
   public ParentOneToMany getParent()
   {
      return _parent;
   }
   ... Other getter/setters removed for brevity ...
}


Code between sessionFactory.openSession() and session.close():
Code:
ParentOneToMany parent = new ParentOneToMany();
parent.setName("Parent");
      
Set<ChildOneToMany> children = new HashSet<ChildOneToMany>();
      
for(int i = 1; i <= 5; i++)
{
   ChildOneToMany child = new ChildOneToMany();
   child.setName("Child" + i);
   child.setParent(parent);
   children.add(child);
}
      
parent.setChildren(children);
session.save(parent);


Log file
Code:
16:44:53,193 DEBUG DefaultSaveOrUpdateEventListener:161 - saving transient instance
16:44:53,193 DEBUG AbstractBatcher:358 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
16:44:53,193 DEBUG SQL:393 - select PARENT_SEQ.nextval from dual
16:44:53,193 DEBUG AbstractBatcher:476 - preparing statement
16:44:53,474 DEBUG SequenceGenerator:82 - Sequence identifier generated: 5
16:44:53,474 DEBUG AbstractBatcher:366 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
16:44:53,474 DEBUG AbstractBatcher:525 - closing statement
16:44:53,474 DEBUG SequenceHiLoGenerator:57 - new hi value: 5
16:44:53,490 DEBUG AbstractSaveEventListener:113 - generated identifier: 250, using strategy: org.hibernate.id.SequenceHiLoGenerator


TIA,
Brett


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 25, 2006 3:31 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
SequenceHiLoGenerator is the Java class named handling sequence generation, it has noting to do with the actual sequence name

_________________
Emmanuel


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

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.