2

I have created a trigger so that my entities ids are autogenerated with a sequence each time they're inserted into my Oracle database.

The problem comes with annotating these entities for Hibernate/JPA: I need to define a @GeneratedValue annotation but I don't want to specify the sequence name -- doing that will make Hibernate query the sequence first, then insert, which is a work that is already done by the trigger.

Is there any way to skip this sequence in the @GeneratedValue with the scenario I've proposed?

Exception I get if id is not provided:

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): Pattern

Pattern class:

@Entity
@Table(name = "PATTERN")
public class Patron extends HistoricoAbstractEntity {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ID_PATTERN")
    private Integer idPattern;

    @Column
    private String description;

    @Column(name = "NEGATIVE")
    private Boolean isNegative;

    @Column(name = "type")
    private Integer type;

    @Column(name = "N_DAYS")
    private Integer numDays;
        ... (getters & setters)

    }
M Rajoy
  • 4,028
  • 14
  • 54
  • 111
  • Would you please tell what is the need of using @GeneratedValue, if you are already generating the id using your implementation. – Bilbo Baggins May 14 '14 at 10:49
  • If I don't specify an id, Hibernate tells me I cannot use the `save` method. – M Rajoy May 14 '14 at 10:50
  • you need to specify an id but there is no need to use @GeneratedValue, use @ Id if you won't specify @ Id in your entity then hibernate will complain. – Bilbo Baggins May 14 '14 at 10:52
  • I used the @Id annotation yet Hibernate will complain if I don't provide an identifier for the entities. – M Rajoy May 14 '14 at 11:20
  • can you post the complain message and your entity class? – Bilbo Baggins May 14 '14 at 11:28
  • This is similar to http://stackoverflow.com/questions/11825643/configure-jpa-to-let-postgresql-generate-the-primary-key-value. The key thing here is @Column(updatable=false) which seems to be the only way forcing hibernate not to assign id itself. – makasprzak May 14 '14 at 17:10

1 Answers1

0

From what your code, What I can tell you is that its not related to @GeneratedValue, it specifies that the hibernate takes responsibility to generate and idetifier for your entity. In your case your are generating id your self, so you have to manually set the id for that particular entity. Then you won't get this error any more, the other thing that you can try is use of @PrePersist annotate a method with this and try assigning a value to id in it. I haven't tried this but this should work according to this answer on SO.

Assign Custom Identifier

If your id is being generated by the database then you should use @GeneratedValue(strategy=GenerationType.AUTO) on your id field along with your @Id annotation.

Community
  • 1
  • 1
Bilbo Baggins
  • 2,899
  • 10
  • 52
  • 77
  • I can't assign an Id, this is a new identity. Id assignation is done in the database itself, so I cannot know the id until the object has been persisted. – M Rajoy May 14 '14 at 11:42