1

I'm currently working on a web app project using java Play 2.3.2 framwork and spring data jpa using postgresql database. I've installed the play sample from activator called play-spring-data-jpa and can be found here: play-spring-data-jpa
The problem I'm facing is to auto generate the table, this feature for some reason is not working displaying the following Error:

[error] o.h.t.h.SchemaUpdate - Unsuccessful: create table public.Person (id bigint generated by default as identity, firstname varchar(255), surname varchar(255), primary key (id))
[error] o.h.t.h.SchemaUpdate - ERROR: syntax error at or near "generated"
Position: 39
[info] play - Application started (Dev)
[error] o.h.u.JDBCExceptionReporter - ERROR: relation "public.person" does not exist
Position: 13
[error] play - Cannot invoke the action, eventually got an error:  javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not insert:[models.Person]
[error] application - 

my persistance.xml:

<persistence 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"
         version="2.0">

<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>DefaultDS</non-jta-data-source>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>

my application.conf:

db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost:5432/MyTest"
db.default.user=postgres
db.default.password="2580"
db.default.jndiName=DefaultDS
applyEvolutions.default=true

When I create the table my self everything goes normal. I dont want to recreate the table my self each time I delete it.

Update: Removing Generated has solved the issue. However, id has to be manually inserted! How can I auto generate an id?

2nd update: problem was solved with the help of this stackoverflow link

Community
  • 1
  • 1

2 Answers2

1

Its creating problem as you using generated by default as identity

remove it and it will work fine

Try the below query

CREATE TABLE attendance.Person (id BIGINT , firstname VARCHAR(255), surname VARCHAR(255), PRIMARY KEY (id))
prashant thakre
  • 5,061
  • 3
  • 26
  • 39
  • You are right, for some reason Genrated annotation is causing an error. I'm not sure why, but could it be the Db it self? Or the driver? I need to figure out another way to implement this feature! Do you have any suggestions? – Salman Ahmed Aug 15 '14 at 16:14
  • why you want to use `Genrated annotation` even without using it , it will work fine. – prashant thakre Aug 15 '14 at 16:39
0

We also faced the same issue. We were having create in the xml and @GeneratedValue on the id column. The resolution is remove the @GeneratedValue annotation and put the value of the id manually, also the jpa takes long by default so give long value e.g 1l.

To do the auto generation follow some another rule.

The issue around the JPA related auto generated Id is resolved as below:

Modify the Person.java model class to have the following annotations for the Id attribute:

@Id @TableGenerator(name="TABLE_GEN",table="T_GENERATOR",pkColumnName="GEN_KEY",pkColumnValue="TEST",valueColumnName="GEN_VALUE",initialValue=1,allocationSize=1) @GeneratedValue(strategy=GenerationType.TABLE, generator="TABLE_GEN") public Long Id;

This will create a table in the mysql schema called T_GNERATOR which will have the tracking of the next value for Id and JPA over hibernate knows how to retrieve this value. The assumtion is that the initial value for the Id is 1 and it is incremented by 1 on each new insertion into it as is obvious from the attributes of the annotation.

Avinav Mishra
  • 718
  • 9
  • 12