Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had the same problem. I am not sure if the root-cause is the same. But there turned to be a perfectly logical explanation. So here is what I discovered.</p> <p>First of all, there are at least 2 distinct ways to create auto-incremented keys.</p> <h2><strong>1st way: (xml)</strong></h2> <p>If you work with an xml-based configuration that holds your class info. Then you can put something as follows in your classname.hbm.xml file.</p> <pre><code>&lt;id name="id"&gt; &lt;generator class="sequence"&gt;&lt;param name="sequence"&gt;my_id_seq&lt;/param&lt;/generator&gt; &lt;/id&gt; </code></pre> <p>To import this file you will have something like the following in your hibernate.cfg.xml file: (or possibly with a resource attribute)</p> <pre><code>&lt;!-- Mapping files --&gt; &lt;mapping file="classname.hbm.xml"/&gt; </code></pre> <p>But the important thing here is that it is now actually JAVA that will increment the keys. If you would have checked the sql that was used to generate the table, you would have noticed that it did not hold an auto-incremented field definition for the id column.</p> <h2><strong>2nd way: (annotations)</strong></h2> <p>A totally different way of doing things is to put everything in annotations, like you showed in your question.</p> <pre><code>@GeneratedValue(strategy=GenerationType.IDENTITY) </code></pre> <p>in your hibernate.cfg.xml file you will have something like:</p> <pre><code>&lt;!-- Mapping files --&gt; &lt;mapping class="package.subpackage.MyClassName"/&gt; </code></pre> <p>The GenerationType.IDENTITY is indeed the default value, so you do not have to supply it per se. But anyway this time the table will be generated differently. Namely as follows:</p> <pre><code>CREATE CACHED TABLE PUBLIC.MYTABLENAME( ID INTEGER DEFAULT (NEXT VALUE FOR PUBLIC.SYSTEM_SEQUENCE_9DE2A0D5_28F5_488F_9E4C_2878B3CDA72F) NOT NULL NULL_TO_DEFAULT SEQUENCE PUBLIC.SYSTEM_SEQUENCE_9DE2A0D5_28F5_488F_9E4C_2878B3CDA72F, ... ) </code></pre> <p>Aha! That's interesting. This time the sequence generation will not be performed by JAVA it will be performed by the database itself.</p> <h2><strong>What went wrong:</strong></h2> <p>We are all experimenting and trying things out of course. If you first decided to do it with the xml-files and afterwards you decided to do it with annotations after all. Then of course that means you will have to regenerate your tables as well. If you forget to do so, then you will get errors like doniyor did.</p> <h2><strong>How to fix it:</strong></h2> <p>just add the following line to your hibernate.cfg.xml and reboot your application.</p> <pre><code>&lt;!-- Drop and re-create the database schema on startup --&gt; &lt;property name="hibernate.hbm2ddl.auto"&gt;create&lt;/property&gt; </code></pre> <p>The table has been destroyed and regenerated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload