2015-09-06 5 views
9

तो मैंने अभी हाइबरनेट 5.0.0.1 डाउनलोड किया है, और मैंने अपनी प्रोजेक्ट को आजमाया है, जो पहले हाइबरनेट 4.3 का उपयोग कर रहा है।हाइबरनेटेट खाली तालिका बना रहा है - hibernate_sequence स्टार्टअप

मैं डेटाबेस के लिए सम्मिलित करते हैं, यह मुझे इस त्रुटि देता है:

ERROR: could not read a hi value - you need to populate the table: hibernate_sequence

मैं mysql का उपयोग कर रहा है, और मेरी पीढ़ी रणनीति GenerationType.auto पर सेट किया जाता है, और ऐसा लगता है कि अब हाइबरनेट दृश्यों का उपयोग कर सोचता है मूल्यों को उत्पन्न करने के लिए सबसे अच्छी रणनीति। लेकिन तालिका खाली है। मुझे लगता है कि हाइबरनेट अनुक्रम से मूल्य प्राप्त करने का प्रयास कर रहा है लेकिन कोई भी नहीं ढूंढ सकता है। लेकिन मैं उलझन में हूं क्योंकि hibernate_sequence हाइबरनेट द्वारा बनाया गया है, क्या यह प्रारंभिक मान प्रदान नहीं करना चाहिए?

उत्तर

15

अनुक्रम तालिका इस कारण है कि आपने अपनी सभी इकाइयों पर प्राथमिक कुंजी को कैसे परिभाषित किया है।

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) // or GenerationType.SEQUENCE 
protected Long id; 

आप एक मेज की पहचान स्तंभ का उपयोग करना चाहते हैं:

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
protected Long id; 

संभव है कि आप अधिक जानकारी के लिए इस सूत्र की जाँच करने के: https://forums.hibernate.org/viewtopic.php?f=1&t=999785&start=0

@GeneratedValue JPA Annotation

Quite often in these tutorials, we have used the @GeneratedValue annotation to have thedatabase generate a unique primary key for us. We have used the default Generation Type in each of our examples, but there are actually four different strategies for having the primary key generated by the database. Those four options are:

AUTO IDENTITY TABLE SEQUENCE javax.persistence.GenerationType.AUTO

The AUTO generation strategy is the default, and this setting simply chooses the primary key generation strategy that is the default for the database in question, which quite typically is IDENTITY, although it might be TABLE or SEQUENCE depending upon how the database is configured. The AUTO strategy is typically recommended, as it makes your code and your applications most portable.

javax.persistence.GenerationType.IDENTITY

The IDENTITY option simply allows the database to generate a unique primary key for your application. No sequence or table is used to maintain the primary key information, but instead, the database will just pick an appropriate, unique number for Hibernate to assign to the primary key of the entity. With MySQL, the first lowest numbered primary key available in the table in question is chosen, although this behavior may differ from database to database.

javax.persistence.GenerationType.Sequence

Some database vendors support the use of a database sequence object for maintaining primary keys. To use a sequence, you set the GenerationType strategy to SEQUENCE, specify the name of the generator annotation, and then provide the @SequenceGenerator annotation that has attributes for defining both the name of the sequence annotation, and the name of the actual sequence object in the database.

+1

लेकिन मेरी रणनीति ऑटो के लिए सेट है, और मुझे लगता है कि मैं करूंगा हाइबरनेट सोचता है कि सबसे अच्छी रणनीति है। लेकिन मैं हाइबरनेट के माध्यम से अनुक्रम कैसे बना सकता हूं? – morbidCode

+2

उपरोक्त उद्धरण में अंतिम आइटम यह सब कहता है। ईमानदारी से, ऑटो का उपयोग न करें जब तक कि पहचान आपके लिए काम न करे। अधिकांश डेटाबेस प्रति-टेबल अद्वितीय पहचानकर्ताओं का समर्थन करते हैं। –

1

Simple solution :

रूप hibernate_sequence तालिका बनाने :

"create sequence <schema/db>.hibernate_sequence" 
1

यह बनाया गया है, क्योंकि है कि तालिका का उपयोग कर हाइबरनेट ऑटो वेतन वृद्धि आईडी (आईडी अगला मूल्य) को ट्रैक करने के

पूर्व-

@Id 
@GeneratedValue 
int id; 
संबंधित मुद्दे