Hi,
I tried to the TableGenerator annotation from hibernate to generate ids. However it does not seem to work.
My entity:
Code:
@Entity
public class TableIdEntity3 {
@TableGenerator(name = "id_gen", table = "custom_sequences", pkColumnName = "custom_name", pkColumnValue="test", valueColumnName = "custom_next_hi_value", initialValue=100)
@Id
@GeneratedValue(generator="id_gen")
private Long id;
private String someField;
public TableIdEntity3(String someField) {
this.someField = someField;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getSomeField() {
return someField;
}
public void setSomeField(String someField) {
this.someField = someField;
}
}
Hibernate will not create the table "custom_sequences", when creating the schema. It simply sets the id column to auto_increment.
perisistence.xml
Quote:
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/jpa" />
<property name="hibernate.show_sql" value="true"/>
</properties>
Testcode:
Code:
EntityTransaction transaction = em.getTransaction();
transaction.begin();
for (int i = 0; i < 5; i++) {
TableIdEntity3 idEntity = new TableIdEntity3("table3" + i);
em.persist(idEntity);
System.out.println("Generated id " + idEntity.getId());
}
transaction.commit();