native generator class in hibernate
Asked Answered
C

2

12

I have this part of hibernate mapping xml file, and I was looking for a good example for what does native mean.

<hibernate-mapping>
 <class name="com.hib.Task" table="tasks">
  <id name="id" type="int" column="id" >
   <generator class="native"/>
  </id>

I know it's something related to unique identifier property, but I would really like to have an example.

Sorry for the newbie question, I'm new to hibernate and programming in general :) Thank you!

Cundiff answered 4/5, 2013 at 9:59 Comment(0)
T
12

Native means Your generator will use identity or sequence columns according to what your current database support.

Docs explained about each strategy here

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html#mapping-declaration-id

native

selects identity, sequence or hilo depending upon the capabilities of the underlying database.

assigned

lets the application assign an identifier to the object before save() is called. This is the default strategy if no element is specified.

For example: In Mysql if you have primary key column as a auto_increment, the db will be updated using this strategy

Tummy answered 4/5, 2013 at 10:4 Comment(3)
"assigned" is the default strategy for generating the primary. For ref key.java4s.com/hibernate/generators-in-hibernateChartulary
Read the 3rd line and that may decide who is nut here.Chartulary
@AshishBurnwal Agree with you Ashish. Sorry for linking some random online resource aswell. Edited to point official docs now. Thanks again.Tummy
A
3

And to complete what Suresh Atta said, you can name the sequence:

<hibernate-mapping>
 <class name="com.hib.Task" table="tasks">
  <id name="id" type="int" column="id" >
   <generator class="native">
     <param name="sequence">s_tasks</param>
   </generator>
  </id>

So it will either work for IDENTITY and for SEQUENCE incremented primary key.

Actinium answered 21/11, 2014 at 9:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.