Reputation: 12423
I want to use SEQUENCE strategy for automatically generate ids, but i am breaking my head to make it work. I dont know at all why i cant make it happen.
This is what i do. First i have an entity:
@Entity
@SequenceGenerator(name="VlasnikSeq", sequenceName="VLA_SEQ")
public class Vlasnik implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="VlasnikSeq")
private Long id;
//...
in the persistence.xml i have it mapped:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="sampleAplication">
<class>entities.Vlasnik</class>
<class>entities.Ljubimac</class>
</persistence-unit>
</persistence>
When i use eclipses feature for generating tables for entities i get this:
As you can see the tables are created, but there is no SEQUENCE table. I also noticed in the console, the following message, while JPA was creating the tables:
[EL Warning]: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLSyntaxErrorException: SEQUENCE 'VLA_SEQ' does not exist.
The next think i do is try to run the app and see if i can create some rows. But when i try to persist something, i get an exception that says:
org.apache.derby.client.am.SqlException: SEQUENCE 'VLA_SEQ' does not
So i come to the conclusion that for some reason i need that table, so i go to the database management perspective and i try to execute the following query:
CREATE SEQUENCE VLA_SEQ;
But i get the following message:
Sequence 'VLA_SEQ' already exists.
I am totally confused. I don't know what should i do. I just want to automatically generate the IDs of my entities when a new row is created in the DB.
It is the first time i use glassfish 3.1, in the version 3.0 i dont remember, having this issue, i could even use just @GenneratedValue. Ill appreciate some help.
Upvotes: 3
Views: 10956
Reputation: 11475
In order to use GeneratedValue you have to specify the strategy and the generator. By default the strategy is AUTO and the generator is empty.
The avaliable strategies are defined by GenerationType:
public enum GenerationType { TABLE, SEQUENCE, IDENTITY, AUTO };
The most common way to use it is by specifying SEQUENCE or IDENTITY.
For databases that use SEQUENCE such as Oracle and HSQLDB you also have to use SequenceGenerator annotation:
public class MyClass {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQMYCLASSID")
@SequenceGenerator(name="SEQMYCLASSID", sequenceName="SEQMYCLASSID")
private Long id;
}
If you have automatic DDL enable you don't have to create the sequence because the JPA provider will do it for you. If that's not the case you have to do it manually like this:
CREATE SEQUENCE SEQMYCLASSID;
For databases that don't use squences and use identity columns such as Microsoft SQL Server you need to specify IDENTITY as strategy:
public class MyClass {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
}
Upvotes: 7