Hibernate - Element:-: Implementations. There Are Shortcut Names For The Built-In Generators
Hibernate - Element:-: Implementations. There Are Shortcut Names For The Built-In Generators
<generator /> is one of main element we are using in the hibernate framework [in
the mapping file], let us see the concept behind this generators.
The optional <generator> child element names a Java class used to generate
unique identifiers for instances of the persistent class. If any parameters are
required to configure or initialize the generator instance, they are passed using the
<param> element.
<generator class="org.hibernate.id.TableHiLoGenerator">
</generator>
</id>
increment generates identifiers of type long, short or int that are unique only when
no other process is inserting data into the same table. Do not use in a
cluster. .
identity supports identity columns in DB2, MySQL, MS SQL Server, Sybase and
HypersonicSQL. The returned identifier is of type long, short or int.
hilo uses a hi/lo algorithm to efficiently generate identifiers of type long, short
or int, given a table and column (by default hibernate_unique_key and
next_hi respectively) as a source of hi values. The hi/lo algorithm
generates identifiers that are unique only for a particular database.
assigned lets the application to assign an identifier to the object before save() is
called. This is the default strategy if no <generator> element is
specified.
PreparedStatement ps = connection
.prepareStatement("SELECT nextval ('seq_emp_code') as nextval");
ResultSet rs = ps.executeQuery();
if (rs.next()) {
int id = rs.getInt("nextval");
String code = prefix + StringUtils.leftPad("" + id,3, '0');
return code;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}