Folks- I am trying to setup a Hibernate+HSQL application (Actually JPA/Hibernate). It has just one entity, Faculty, a 'hello world' type application. I was getting the following error:
Code:
09:13:21,971 ERROR SchemaUpdate:212 - Unsuccessful: create table FACULTY (id numeric(19,0) identity not null, FIRST_NAME varchar(255) null, LAST_NAME varchar(255) null, primary key (id))
09:13:21,972 ERROR SchemaUpdate:213 - primary key already exist
After a lot of wasted efforts to find cure on the net, I pasted the above (hibernate generated) SQL into the Swing HSQL Database Manager and tried to run it there. Got the same error message, "primary key already exist".
Next I changed the SQL so that there was no primary key mentioned:
Code:
create table FACULTY (id numeric(19,0) identity not null, FIRST_NAME varchar(255) null, LAST_NAME varchar(255) null)
Not only did the DDL run fine, it also created an index (SYS_IDX_SYS_PK_10174_10175) on the ID column.
Here is what I surmised:
HSQL (at least the version I'm using) automatically creates a primary index on a field marked as Identity.
Hibernate is having a problem with its dialect property, it is not 'understanding' the dialect, it should not try to explicitly create a primary key.
Since the table, Faculty was created successfully, next time I reloaded by application, it ran just fine and was able to insert records.
Is this a known problem or I am doing something wrong?
I'll appreciate any help.
persistence.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/testDS</jta-data-source>
<properties>
<!-- <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" /> -->
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>
Entity:
Code:
package com.test.domain;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="FACULTY")
public class Faculty implements Serializable {
/**
*
*/
private static final long serialVersionUID = -4180281348406187570L;
private Long id = null;
private String firstName = null;
private String lastName = null;
public Faculty() {}
public Faculty(String fname, String lname) {
firstName = fname;
lastName = lname;
}
/**
* Gets id (primary key).
*/
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
/**
* Sets id (primary key).
*/
public void setId(Long id) {
this.id = id;
}
/**
* Gets first name.
*/
@Column(name = "FIRST_NAME")
public String getFirstName() {
return firstName;
}
/**
* Sets first name.
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* Gets last name.
*/
@Column(name = "LAST_NAME")
public String getLastName() {
return lastName;
}
/**
* Sets last name.
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
}