Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Object Relational Mapping
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Object Relational Mapping
[Hibernate] Problem with @TableGenerator
Daniel Suter
Greenhorn
Posts: 2
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
I tried to the TableGenerator annotation from hibernate to generate ids. However it does not seem to work.
My entity:
@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
<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:
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();
Bill Gorder
Bartender
Posts: 1682
7
I like...
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
make sure all of your imports are coming out of javax.persistence.
also change to
@GeneratedValue(strategy = GenerationType.TABLE, generator = "id_gen")
See if that makes a difference for you.
[
How To Ask Questions
][
Read before you PM me
]
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
EJB3 Beta Certification - EntityManager BASIC notes. (PART II)
Query on various properties wrt auto table generator
Map multiple properties as @Id properties Problem
Identity issue for EJB3.0 Entity along with Hibernate HBM configuration
org.hibernate.exception.GenericJDBCException
More...