I have created a sequence called as hibernate_sequnce using following command
create sequence hibernate_sequence start with 400;
This is my hibernate annotated dao
#Entity
#Table(name="T_BIZ_TERM")
public class BizTerm implements Serializable {
/**
*
*/
private static final long serialVersionUID = -3056055722354292136L;
private Long bizTermId;
#Id
#GeneratedValue()
#Column(name="BIZ_TERM_ID")
public Long getBizTermId() {
return bizTermId;
}
public void setBizTermId(Long bizTermId) {
this.bizTermId = bizTermId;
}
}
In order to insert new record into the table hibernate is generating wrong query to get next sequence value..
Hibernate is always issuing this query..no matter what I do..
select nextval(hibernate_sequnce)
I am using oracle 10G which is reporting following error always..
ORA-00923: FROM keyword not found where expected
The query issued should be select hibernate_sequnce.nextval from dual;
what to do now, am I missing anything?
I do something like this for Oracle sequences.
#Id
#Column(name = "BIZ_TERM_ID")
#SequenceGenerator(name = "myKeySeq", sequenceName = "hibernate_sequence ", allocationSize = 20)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "myKeySeq")
private Long bizTermId;;
I'd name the sequence better than hibernate_sequence though. try something like biz_term_sequence. You will want different sequences per table primary key.
Did you configure the Oracle dialect?
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
Related
I have an entity mapped and I want to verbosely point it to its sequence. So I have the following mapping:
#Entity
#Table(schema = DbConstants.SCHEMA_PUBLIC, name =
DbConstants.PUBLIC_TABLE_TEST)
public class TestEntity implements Serializable {
private static final long serialVersionUID = 6284010266557287786L;
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "test")
#SequenceGenerator(schema = DbConstants.SCHEMA_PUBLIC, name = "test", sequenceName = "test_id_seq")
private Integer id;
}
The problem is that if I don't include allocationSize in the #SequenceGenerator annotation the entities seem to have an id of something along the lines of -49 + nextval('test_id_seq'::regclass). Adding a new entity also increments the test_id_seq. I've compared the sql that hibernate uses with or without the allocationSize and it's exactly the same.
1) How does this happen?
2) How do the database ids end up being so different with seemingly the same sql being used by hibernate?
I use Liquibase to create the DDL schema of the database(Derby). Also, I use JPA and EclipseLink and I want to make EclipseLink so that it does not insert any values for primary keys and I want them to be generated through the pure sql. Now, I`ve tried to remove the generation type strategy in the entities, but it is trying to insert null values for PKs to the tables, which are not allowed for PKs.
I`ll be glad if you help me.
Now I have this, but it gives me the exception below.
#Entity
#XmlRootElement
#Table(name = "ROLE")
public class Role implements Serializable {
private static final long serialVersionUID = 4736444799522006644L;
# Id
# JsonIgnore
# GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID")
private Long id;
AND
CREATE TABLE Role (
id BIGINT PRIMARY KEY NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
role VARCHAR(255)
);
Exception Description: The attribute [id] of class [...] is mapped to a primary key column in the database. Updates are not allowed.
You could create a sequence for generating IDs (instead of this GENERATED BY DEFAULT START WITH 1, INCREMENT BY 1) . Its' a better approach and after that you can use your sequence in JPA:
...
#Entity
public class Employee {
#Id
#GeneratedValue(strategy=GenerationType.SEQUENCE, generator="EMP_SEQ")
#SequenceGenerator(name="EMP_SEQ", sequenceName="EMP_SEQ", allocationSize=100)
private long id;
...
}
You can use sequence in SQL <sequence>.NEXTVAL to get new ID value.
For more info - please check https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing
Unfortunately this method won't work if your DB does not support sequences.
Or for your Derby DB you can try this (if primary key is generated as identity)
#Entity
public class EntityWithIdentityId {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
....
}
I am using hibernate annotations. I want to make a column as autoincremented. I have created a sequence in database(oracle) and mapped that sequence in java POJO class. Do I need to create trigger for that sequence too? I want to know how we can make a column autoincremented while using hibernate anotation? What changes i have to do in java and as well as database side? Please help me in this. Following is the part of code where I have mapped the sequence.
public class SimRuns implements Serializable {
private static final long serialVersionUID = 8698324570356602407L;
#Id #Column(name = "RUN_ID")
#GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_run_id")
#SequenceGenerator(name="seq_run_id", sequenceName="seq_run_id")
private Long runId;
}
This works for me:
#Id
#GeneratedValue(generator = "nosicSeq")
#SequenceGenerator(name = "nosicSeq", sequenceName = "NOSIC_SEQ", allocationSize = 1)
#Column(name = "SID")
private BigDecimal sid;
no triggers in DB needed, just sequence.
Try removing the generator and setting to auto the generationType
#Id #Column(name = "RUN_ID")
#GeneratedValue(strategy=GenerationType.AUTO)
#SequenceGenerator(name="seq_run_id", sequenceName="seq_run_id")
private Long runId;
Hibernate is probably the only JPA Provider that selects the right Generationstrategy based on your databasetype. Using the GenerationType.AUTO statement hibernate will try to select the best strategy to implement an increasing row id.
In Oracle : It's no need for an Oracle trigger, but the sequence is must.
In pojo: you can use Annotations like this:
#Id
#GeneratedValue(strategy=GenerationType.SEQUENCE, generator="majorsSeq")
#SequenceGenerator(name="majorsSeq", sequenceName="MAJORS_SEQ", allocationSize = 1,initialValue = 1)
public int getId() {
return id;
}
I am using Spring 3 and Hibernate 4
I have the following in Entity class
#Id
#Column(name = "PROJECT_NO")
private String projectNumber;
When I insert values into database table, is it possible to insert PROJECT_NO value which is the Primary key in the following format
20131 where 2013 is the current year and next character should be incremental by one. i.e. next value to be inserted should be 20132
How can I achieve this using JPA/Hibernate
You have to look at #GeneratedValue and #GenericGenerator annotation.
There are several possibilities to generate value. In your case I believe the you need to create something like the following:
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "year_gen")
#GenericGenerator(name = "year_gen", strategy = "com.example.generator.CustomGenerator")
#Column(name = "PROJECT_NO")
private String projectNumber;
And CustomGenerator should implement org.hibernate.id.IdentifierGenerator
What about putting the JPA annotation on the getter and using the getter method to format the data as you like?
#Entity
public class MyClass {
private String projectNumber;
#Column(name = "PROJECT_NO")
public String getProjectNumber(){
return doSomeFormatting(this.projectNumber);
}
}
Something like that should work.
I wanted to to generate sequence using hibernate tool ( pojo to sql). And definitely it works fine.
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqid-gen")
#SequenceGenerator(name = "seqid-gen", sequenceName = "RTDS_ADSINPUT_SEQ" )
#Column(name="id")
public Long getId() {
return id;
}
This code generates below sql
create sequence RTDS_ADSINPUT_SEQ;
The problem is I wanted to specify properties like
INCREMENT BY,NOCACHE CYCLE
and the final ddl script should be some thing like below
CREATE SEQUENCE RTDS_ADSINPUT_SEQ MINVALUE 1 MAXVALUE
999999999999999999 INCREMENT BY 1 START WITH 1 NOCACHE ORDER CYCLE ;
But as far I saw hibernate only support name, sequncename,allocation,initialvalue
Please advice me if I can include those properties as annotation in the pojo.
I looked it up in the Hibernate sources (4.2.7). It is not possible to specify this with an annotation (neither JPA nor Hibernate).
However you can provide your own Dialect to achieve this.
public class MyOwnOracleDialect extends Oracle10gDialect {
#Override
protected String getCreateSequenceString(final String sequenceName, final int initialValue, final int incrementSize)
throws MappingException {
String createSequenceString = super.getCreateSequenceString(sequenceName, initialValue, incrementSize);
return createSequenceString + " NOCACHE ORDER CYCLE"; // modify this string as you like
}
}
Have an entity like this
#Entity
public class MyEntity {
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqid-gen")
#SequenceGenerator(name = "seqid-gen", sequenceName = "RTDS_ADSINPUT_SEQ", allocationSize = 1, initialValue = 0)
#Column(name="id")
private Long id;
// ...
}
You can set your new Dialect as described in the Hibernate doc (http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch03.html#configuration-optional-dialects)
I think you are looking for something like this
<id name="pk_field" column="column_name">
<generator class="sequence">
<param name="sequence">sequence_name</param>
<param name="parameters">START WITH 5 INCREMENT BY 10</param>
</generator>
</id>