Search code examples
springhibernatepostgresqljpaspring-roo

Postgresql and Spring Roo Binding serial type for ID


I am having trouble using the Postgresql serial type in Spring Roo. What I want is to have the an auto-incrementing id column which will work with the auto generated entity classes in Roo.

The Postgresql sequences, which are generated with the default way of doing things in Spring Roo, work fine within the spring application. But sometimes I have to manually insert rows in the database using sql. (the sequences dont seem to work properly when I do an INSERT INTO... statement). If I could use serial type, then manual INSERTS are easy.

For example I have an office entity and and employee entity. There is a many-to-one relationship between employees and offices.

Here is my class for the Office entity.

@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Office {

    @Id
    @Column(name="officeid", columnDefinition = "serial")
    @Generated(GenerationTime.INSERT)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long officeid;


    /**
    * Office Name
    */
    @NotNull
    @Size(max = 100)
    private String name;
}

While this does work when my test inserts an office record, it fails when an employee record is inserted since the officeid foreign key value is null. (I guess it needs to flush between the office insert and the employee insert, but the auto-generate tests dont seem to do that.)

So what is the proper annotations to use to tell Roo (and hibernate/jpa) to use the serial data type, and also to work properly with inserts and relationships within the spring application?


Solution

  • Roo generates default JPA annotations, you must customize and setup them as needed. Note Roo guarantees your changes won't be modified.