-->
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.  [ 4 posts ] 
Author Message
 Post subject: Annotation Id GeneratorType.AUTO and SchemaExport
PostPosted: Wed Mar 16, 2005 5:38 pm 
Newbie

Joined: Wed Mar 16, 2005 5:04 pm
Posts: 3
Location: Argentina
Hibernate version: Hibernate 3.0 rc1

In hibernate 2.x when using the id generator "native" with this configuration:

<id name="id" column="id" type="java.lang.Long" unsaved-value="null">
<generator class="native">
<param name="sequence">seq_foo_name</param>
</generator>
</id>


The old schema export tool generates a sequence named "seq_foo_name" in the DDL script.
Now with the annotations I can't find the way to specify the sequence name.

the way I run the SchemaExport:

Code:
public static void main(String[] args) {
        Configuration cfg = new AnnotationConfiguration()
                .addAnnotatedClass(Patient.class)
                .addAnnotatedClass(Gender.class);
        Properties props = new Properties();
        props.setProperty(Environment.USER, "test");
        props.setProperty(Environment.PASS, "*****");
        props.setProperty(Environment.DIALECT, PostgreSQLDialect.class.getName());
        props.setProperty(Environment.URL, "jdbc:postgresql://server/dbTest");
        props.setProperty(Environment.DRIVER, Driver.class.getName());
        props.setProperty(Environment.SHOW_SQL, "true");
       
        SchemaExport schemaExport = new SchemaExport(cfg, props);
        schemaExport.setOutputFile("c:/ddl.txt");
        schemaExport.create(false, true);
    }



this are the examples I've tried:

example 1:
Code in Patient Class:
Code:
@Id(generate=GeneratorType.AUTO)
@SequenceGenerator(name="gen_name", sequenceName="seq_foo_name")
public Long getId() {
   return id;
}


this doesn't produce errors but in the DDL generates the following line:
create sequence hibernate_sequence


example 2:

Code in Patient Class:
Code:
@Id(generate=GeneratorType.AUTO, generator="gen_name")
@SequenceGenerator(name="gen_name", sequenceName="seq_foo_name")
public Long getId() {
   return id;
}


this produces the following error:
16/03/2005 18:03:45 org.hibernate.cfg.AnnotationConfiguration addAnnotatedClass
FATAL: Could not compile the mapping annotations
org.hibernate.AnnotationException: Incompatible generator between Id.generate and its named generator: native!=gen_name
at org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:957)

_________________
--
Bauna


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 6:31 pm 
Newbie

Joined: Wed Mar 16, 2005 5:04 pm
Posts: 3
Location: Argentina
I made a path to example 2 works:

Index: org/hibernate/cfg/AnnotationBinder.java
===================================================================
RCS file: /cvsroot/hibernate/HibernateExt/metadata/src/java/org/hibernate/cfg/AnnotationBinder.java,v
retrieving revision 1.64
diff -u -r1.64 AnnotationBinder.java
--- org/hibernate/cfg/AnnotationBinder.java 5 Mar 2005 19:58:08 -0000 1.64
+++ org/hibernate/cfg/AnnotationBinder.java 17 Mar 2005 21:40:03 -0000
@@ -918,12 +918,7 @@
if (gen == null) {
throw new AnnotationException("Unknown Id.generator: " + generatorName);
}
- if ( ! gen.getIdentifierGeneratorStrategy().equals(generatorType) ) {
- //named generator and id one should be compatible
- throw new AnnotationException(
- "Incompatible generator between Id.generate and its named generator: "
- + generatorType + "!=" + generatorName);
- }
+ isGeneratorCompatible(gen, generatorType, generatorName);
Iterator genParams = gen.getParams().entrySet().iterator();
while ( genParams.hasNext() ) {
Map.Entry elt = (Map.Entry) genParams.next();
@@ -940,6 +935,25 @@
rootClass.setIdentifierProperty(prop);
}

+ /**
+ * @param gen
+ * @param generatorType
+ * @param generatorName
+ */
+ private static void isGeneratorCompatible(IdGenerator gen, String generatorType, String generatorName) {
+ //this means: the generators are the same type or
+ //Id annotation's generatorType == AUTO and name generator's strategy is SEQUENCE
+ //I made this change to allow specify the name of the sequence
+ if ( ! (gen.getIdentifierGeneratorStrategy().equals(generatorType) ||
+ (generatorType.equals(generatorType(GeneratorType.AUTO)) &&
+ gen.getIdentifierGeneratorStrategy().equals(generatorType(GeneratorType.SEQUENCE))))) {
+ //named generator and id one should be compatible
+ throw new AnnotationException(
+ "Incompatible generator between Id.generate and its named generator: "
+ + generatorType + "!=" + generatorName);
+ }
+ }
+
private static void fillGeneratorWithGeneratorTableParams(Properties params, HashMap<String, Properties> localGeneratorTables, String generatorName, ExtendedMappings mappings) {
final String generatorTableName = params.getProperty(GENERATOR_TABLE_NAME_PARAM);
Properties props = mappings.getGeneratorTableProperties(generatorTableName, localGeneratorTables);

_________________
--
Bauna


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 18, 2005 12:57 am 
Beginner
Beginner

Joined: Mon Dec 15, 2003 5:25 am
Posts: 48
Location: Delhi, India
Try this-

Code:
@Entity
@Table(name="omuser")
@SequenceGenerator(name="SEQ_USER", sequenceName="user_sequence")
public class User implements Serializable
{
  @Id(generate=GeneratorType.SEQUENCE, generator="SEQ_USER")
  @Column (name="user_id")
  public int getId()
  {
    return id;
  }

}


It works for me.

_________________
Vinod K. Singh


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 18, 2005 5:59 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I'm +1 with bauna proposal, I'll basically apply the patch

_________________
Emmanuel


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