-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate: Reverse Engineering: @GeneratedValue - Problem
PostPosted: Tue Jul 15, 2008 3:00 pm 
Newbie

Joined: Tue Jul 15, 2008 2:44 pm
Posts: 3
Location: Switzerland
How can I influence the reverse engineering process in order to generate the following:

Code:
   @Id   
   @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SequenceGenerator")
   @Column(name = "ADDRESS_ID", unique = true, nullable = false)
   public Integer getAddressId() {
      return this.addressId;
   }


The main problem is that there is no way to generate "@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SequenceGenerator")"!
I tried different "hibernate.reveng.xml" configurations but without success.
I searched everywhere on the internet and in the hibernate forum but without success.

Hibernate version:
HibernateTools-3.2.0.GA.zip
Eclipse 3.3.2

Mapping documents:
persistence.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!-- example of a default persistence.xml -->
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
   version="1.0">
   <persistence-unit name="devglobaldb">
        <class>ch.becke.services.technical.model.entity.generated.Address</class>
      <properties>
         <property name="hibernate.dialect"
            value="org.hibernate.dialect.HSQLDialect"></property>
         <property name="hibernate.connection.driver_class"
            value="org.hsqldb.jdbcDriver"></property>
         <property name="hibernate.connection.username"
            value="devglobaldb"></property>
         <property name="hibernate.connection.password"
            value="devglobaldb"></property>
         <property name="hibernate.connection.url"
            value="jdbc:hsqldb:hsql://localhost:6000/devglobaldb"></property>
         <property name="hibernate.default_schema"
            value="VG001_000_000"></property>
      </properties>
   </persistence-unit>
</persistence>


orm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd"
   version="1.0">
   <sequence-generator name="SequenceGenerator" sequence-name="SEQ_GEN"/>
</entity-mappings>



Name and version of the database you are using:
hsqldb_1_8_0_10.zip


Database Table trying to reverse engineer
------------------------------------------------
Code:
CREATE TABLE ADDRESS (
      ADDRESS_ID INTEGER NOT NULL,
      STREET VARCHAR(64),
      STREET_NR VARCHAR(32),
      ZIP VARCHAR(32) NOT NULL,
      ADDRESS_TYPE_KEY VARCHAR(32),
      PERSON_ID VARCHAR(64),
      MANDANT VARCHAR(32),
      VERSION INTEGER
   );
ALTER TABLE ADDRESS ADD CONSTRAINT SYS_PK_49 PRIMARY KEY (ADDRESS_ID);


Eclipse Hibernate Configuration Dialog:
->Main:
---->JPA (JDK1.5+)

Eclipse Code Generation Dialog:
->Main:
---->Reverse Engineer from JDBC Connection: true
---->Generate basic typed composite ids: true
---->Detect optimistic lock columns: true
---->Detect many-to-many tables: true
->Exporters:
---->Use Java 5 syntax: true
---->Generate EJB3 annotations: true
---->Exporters: Domain code (.java)

Thank you for your help - regards
Raoul

_________________
Raoul Becke
Switzerland


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 21, 2008 1:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
have you tried implementing a custom reverse engineering strategy that defines the primary key generation strategy ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Hibernate: Reverse Engineering: @GeneratedValue - Problem
PostPosted: Wed Jul 30, 2008 2:00 pm 
Regular
Regular

Joined: Tue Jul 29, 2008 4:15 pm
Posts: 62
Location: Dallas, TX US
Quote:
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SequenceGenerator")
@Column(name = "ADDRESS_ID", unique = true, nullable = false)
public Integer getAddressId() {
return this.addressId;
}


I am using Oracle XE and I was able to generate the following in my POJOs for my property id:
Quote:
@SequenceGenerator(name = "generator", sequenceName = "JEDI_DEV.COMMAND_TYPE_SEQ")
@Id
@GeneratedValue(strategy = SEQUENCE, generator = "generator")
@Column(name = "ID", unique = true, nullable = false, precision = 22, scale = 0)
public Long getId() {
return this.id;
}


I was able to achieve this my creating a reveng.xml with the following configuration:
Quote:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>
<table-filter match-name="COMMAND"><!-- />-->
<meta attribute="extra-import">com.aviall.edi.modules.order.process.core.models.BaseModel</meta>
<meta attribute="extends">BaseModel</meta>
<meta attribute="class-description">bla blah</meta>
</table-filter>

<table name="COMMAND">
<primary-key>
<!-- setting up a specific id generator for a table -->
<generator class="sequence">
<param name="sequence">COMMAND_SEQ</param>
</generator>

<key-column name="ID" type="Long"/>
</primary-key>
</table>
</hibernate-reverse-engineering>


You should try to generate your sequence annotation this way.

Hope this helps!

_________________
pouncilt


Top
 Profile  
 
 Post subject: Re: Hibernate: Reverse Engineering: @GeneratedValue - Problem
PostPosted: Fri May 20, 2011 4:46 am 
Newbie

Joined: Fri May 20, 2011 3:41 am
Posts: 5
Hi everybody !

I know this post is old but I have the same question as rbecke !

I need to add automatically an @GeneratedValue to all the primaty keys of all my tables. Here is the reason why :
- I managed to generate automatically my own OID using my specific class OIDgenerator.java
Code:
public class OIDgenerator implements IdentifierGenerator {
   public Serializable generate(SessionImplementor arg0, Object obj) throws HibernateException {
      (...)
      return OID;
   }
}

- this class can be used for all the primary keys of the tables/classes of my package, thanks to the package-info.java class :
Code:
@org.hibernate.annotations.GenericGenerator(name="OID", strategy="com.mySociety.myProject.hibernate.OIDgenerator")
package com.mySociety.myProject.bo;

- so now, if I manually add @GeneratedValue before a given primary key, then my OIDgenerator.java class is correctly used to generate the table primary key (when I add new records in the table) :
Code:
package com.mySociety.myProject.bo;

import (...);

@Entity
@Table(name = "T_POR", schema = "SCHEMA")
public class TPor implements java.io.Serializable {

   private String porOid;
   private String porAttr;
   (...)

   public TPor() {
   }

   public TPor(String porOid, String porAttr, (...) ) {
      this.porOid = porOid;
      this.porAttr = porAttr;
      (...)
   }

   public TPor(String porOid, String porAttr, (...) ) {
      this.porOid = porOid;
      this.porAttr = porAttr;
      (...)
   }

   @Id
   @GeneratedValue <<--------------------------------------------------------------HERE---------------
   @Column(name = "POR_OID", unique = true, nullable = false, length = 40)
   public String getPorOid() {
      return this.porOid;
   }

   public void setPorOid(String porOid) {
      this.porOid = porOid;
   }

   @Column(name = "POR_ATTR", nullable = false, length = 3)
   public String getPorAttr() {
      return this.porAttr;
   }

   public void setPorAttr(String porAttr) {
      this.porAttr = porAttr;
   }
   (...)
}

- I found a way to roughly automatically generate something that is close to what I want to do : in the hibernate.reveng.xml file, I added a primary key generator tag :
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>
   <schema-selection match-schema="SCHEMA" />
   (...)
   
   <table-filter match-schema="SCHEMA" match-name=".*"></table-filter>
   (...)
   
   <table schema="SCHEMA" name="T_POR">
      <primary-key>
         <generator class="com.mySociety.myProject.hibernate.OIDgenerator"/> 
      </primary-key>
   </table>
   
</hibernate-reverse-engineering>


When I generate my classes with hibernate, I have this result for the table of the example above :
Code:
   (...)

   @GenericGenerator(name = "generator", strategy = "com.mySociety.myProject.hibernate.OIDgenerator")
   @Id
   @GeneratedValue(generator = "generator")
   @Column(name = "POR_OID", unique = true, nullable = false, length = 40)
   public String getPorOid() {
      return this.porOid;
   }

   (...)


I said this is roughly automatic because I'd need to add the primary key generator tag in the hibernate.reveng.xml file for all the tables of my DB !
And I said it is close to what I want because I don't need to specify again the generator strategy, because it is set by default in all the package (see above).

I tried to use a joker character ( '*', '.*', ...) in the primary key generator tags of the hibernate.reveng.xml file but it failed.
I red about the ReverseEngineeringStrategy classes but I didn't find how to addapt them to my problem ...
Code:
public class ReverseStrategy extends DelegatingReverseEngineeringStrategy {

   public ReverseStrategy(ReverseEngineeringStrategy delegate) {
      super(delegate);
   }   
}


So if someone can help, I'd be grateful :)

Thanks in advance.

[EDIT]I work with Eclipse and hiberate 3.5.6[/EDIT]


Top
 Profile  
 
 Post subject: Re: Hibernate: Reverse Engineering: @GeneratedValue - Problem
PostPosted: Fri Mar 14, 2014 1:08 pm 
Newbie

Joined: Fri Feb 21, 2014 8:48 am
Posts: 4
I have the same issue, since some time passed i wonder if any of you guys found out how to do it... plz post some examples if you did.


Top
 Profile  
 
 Post subject: Re: Hibernate: Reverse Engineering: @GeneratedValue - Problem
PostPosted: Wed May 06, 2015 5:15 am 
Newbie

Joined: Wed May 06, 2015 5:07 am
Posts: 1
Hi,

Has anyone got answer of this question?
I need solution of the same problem "how to automatically add an @GeneratedValue to all the primary keys of all the tables?".


Top
 Profile  
 
 Post subject: Re: Hibernate: Reverse Engineering: @GeneratedValue - Problem
PostPosted: Wed May 06, 2015 8:48 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Provide a ReverseEngineeringStrategy:

Code:
public class ReverseStrategy extends DelegatingReverseEngineeringStrategy {

   public ReverseStrategy(ReverseEngineeringStrategy delegate) {
      super(delegate);
   }

  public String getTableIdentifierStrategyName(TableIdentifier identifier) {
     return "com.mySociety.myProject.hibernate.OIDgenerator";
  }
}


That would make *all* tables have the same id strategy.

If you only want it on some you can use the `identifier` parameter to limit it.

_________________
Max
Don't forget to rate


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