Wednesday, March 10, 2010

hibernate native Id generator tip

Tagged with:
Tuesday, October 14, 2008, 16:38
This news item was posted in HowTo & Tutorial category and has 2 Comments so far.

if you are working with more than one db vendor (mysql/postgresql/oracle ,vs…) both at the same project. you may like this a lot :)

the configuration sample below allow to run your hibernate application at two or more database vendors without changing any code

it will use both  mysql native id generator and different hibernate sequences for each of your mapping objects (not just one hibenate sequence for all objects)

@Entity
@Table(name = "ORM_Company")
public class Company implements java.io.Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "ORM_Company_SEQ")
@SequenceGenerator(name = "ORM_Company_SEQ", sequenceName = "ORM_Company_SEQ")
@Column(name = "id")
private java.lang.Long id = 0L;
//...

}

OR as in old xml style :

<class name="your.pack.Company"  table="ORM_Company" dynamic-update="true" >
<id name="id" type="long" column="id">
<generator class="native" >
<param name="sequence">ORM_Company_SEQ</param>
</generator>
</id>
Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Wists
  • LinkedIn
  • Slashdot
  • Google Bookmarks
  • Reddit
  • StumbleUpon
  • email
  • Twitter

Related posts:

  1. hibernate native id generator bug 3.2.4-ga
  2. hibernate tip: smart id generator
  3. Clean database schema with inheritance
  4. Moving from Tomcat to JBoss
  5. DistributableEventPublisher for Spring via JGroups

You can leave a response, or trackback from your own site.

2 Responses to “hibernate native Id generator tip”

  1. Darren Bell said on Wednesday, October 15, 2008, 4:30

    These are the annotations we use. This is because, by using a uuid, we have a distributable/replicate able database out of the box.

    @Id
    @GeneratedValue(generator = “system-uuid”)
    @GenericGenerator(name = “system-uuid”, strategy = “uuid”)
    @Column(length = 32, name = EntityObject.Columns.ID)
    @DocumentId(name=Attributes.ID)
    private String id;

    Works on every db :)

  2. altuure said on Wednesday, October 15, 2008, 5:50

    thanks darren ,
    this configuration would be more ideal if you would use a guid-string identifier of course

Leave a Reply