-->
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.  [ 6 posts ] 
Author Message
 Post subject: ID generator for legacy database using oracle sequence
PostPosted: Wed May 14, 2008 3:54 pm 
Newbie

Joined: Wed May 14, 2008 3:45 pm
Posts: 8
I am using JPA under JBoss Seam.
I am working on a legacy database schema. The table has varchar2 as the primarykey data type. Even if the datatype is varchar2 the content of that column is always integer type. The legacy application is generating the primary on its own.

I am trying to use the Generator Sequence strategy to generate the primary using the annotation

@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="USER_SEQ")

But I am getting ClassCastException. The reason could be the type mismatch. The sequence generator returns int,long, or short and which is tried to assign to the string property.

Could anybody please show me some direction regarding which generator strategy to use in this scenario.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 14, 2008 4:24 pm 
Newbie

Joined: Wed May 14, 2008 4:14 pm
Posts: 5
Did you remember to create the generator?

I just copy=pasted this from the manual:
http://www.hibernate.org/hib_docs/annot ... le/#entity

<sequence-generator name="USER_SEQ"
sequence-name="my_sequence"
allocation-size="20"/>

//and the annotation equivalent

@javax.persistence.SequenceGenerator(
name="USER_SEQ",
sequenceName="my_sequence",
allocationSize=20
)

SEQ_GEN defines a sequence generator using a sequence named my_sequence. The allocation size used for this sequence based hilo algorithm is 20. Note that this version of Hibernate Annotations does not handle initialValue in the sequence generator. The default allocation size is 50, so if you want to use a sequence and pickup the value each time, you must set the allocation size to 1.

Note
Package level definition is no longer supported by the EJB 3.0 specification. However, you can use the @GenericGenerator at the package level (see Section 2.4.Identifier, “Identifier”).

The next example shows the definition of a sequence generator in a class scope:

@Entity
@javax.persistence.SequenceGenerator(
name="SEQ_STORE",
sequenceName="my_sequence"
)
public class Store implements Serializable {
private Long id;

@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_STORE")
public Long getId() { return id; }
}

This class will use a sequence named my_sequence and the SEQ_STORE generator is not visible in other classes.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 14, 2008 4:51 pm 
Newbie

Joined: Wed May 14, 2008 3:45 pm
Posts: 8
Thank you for the quick reply.

Yeah did like this

@SequenceGenerator(name="USER_SEQ",sequenceName="HSG_SEQUENCE")


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 14, 2008 5:02 pm 
Newbie

Joined: Wed May 14, 2008 4:14 pm
Posts: 5
Im still a newbie to Hibernate, but wouldnt you need a sequence generator table in your DB and then create a class to map this to?

So it looks like this:

You need a new ID->Goes to IDGEN class->Goes to IDGEN DB

Just to get rid of the assumptions, you do have them as well, right?
Or are you using other method Im not aware of?

Sorry for trying to help with my newbieness n_nU


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 3:51 pm 
Newbie

Joined: Wed May 14, 2008 3:45 pm
Posts: 8
I have sequence in database. My question is, since the sequence are just number I don't know how to assign the number in to the hibernate bean property that has datatype of string ( and varchar2 in database table)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 20, 2008 10:31 am 
Newbie

Joined: Wed May 14, 2008 3:45 pm
Posts: 8
I found the solution which is as follows. we need to create a custom sequence generator and use it instead of the hibernate provided one

public class VarcharSequenceGenerator extends SequenceGenerator{

public Serializable generate(SessionImplementor arg0, Object arg1)
throws HibernateException {
Object sequenceResult = super.generate(arg0, arg1);
return sequenceResult == null ? null : sequenceResult.toString();
}


}


And use it as

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="userSequenceGenerator")
@GenericGenerator(name = "userSequenceGenerator",
strategy = "util.domain.VarcharSequenceGenerator",
parameters = {@Parameter(name = "sequence", value = "MY_SEQUENCE")})

@Column(name = "ID", unique = true, nullable = false, insertable = true, length = 40)
public String getId() {
return this.id;
}

public void setId(String id) {
this.id = id;
}


My_SEQUENCE is the actual sequence name in the database. You do not need @SequenceGenerator.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.