Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA @Id and insertable = false, updatable = false throws exception

I'm using Oracle database and I have sequence and trigger for generating and storing ID before insert.

CREATE SEQUENCE CASE_SEQ START WITH 1001 INCREMENT BY 1 NOMAXVALUE; 

CREATE OR REPLACE TRIGGER CASE_TR_SEQ
BEFORE INSERT ON CASE FOR EACH ROW
BEGIN
  SELECT CASE_SEQ.NEXTVAL INTO :NEW.CASE_ID FROM DUAL;
END;
/

Then I have simple entity with property:

@Id
@Column(name = "CASE_ID", insertable = false, updatable = false)
private Long caseId;

...when I try build project I'm getting:

Exception [EclipseLink-46] (Eclipse Persistence Services - 2.3.2.v20111125-r10461):
org.eclipse.persistence.exceptions.DescriptorException
Exception Description: There should be one non-read-only mapping defined for the
primary key field [CASE.CASE_ID].

When I remove either insertable or updatable keyword, then it works. I know that there is a lot of solutions how generate ID using JPA, also JPA can use (call) oracle sequence to set (generated) ID. But I try understand why is my solutions wrong. Why I can't use both keywords together with the @Id annotation? My thought is: I want to prohibit insert or update caseId by JPA.

1) What is the proper soulution? Should I use only @Id:

@Id
@Column(name = "CASE_ID")
private Long caseId;

or is better (safer) define insertable=false also:

@Id
@Column(name = "CASE_ID", insertable = false)
private Long caseId;

2) I understand that updatable=false for @Id does not have meaning (update Primary Key does not have meaning but it is possible by raw sql), but what does it mean (do you have some example when it is beneficial):

@Id
@Column(name = "CASE_ID", updatable = false)
private Long caseId;

EDIT 2012-04-13

I made some tests:

Entity

@Id
@Column(name = "CASE_ID")
private Long caseId;

JPA log

INSERT INTO CASE (CASE_ID, CASE_DATE, INFO) VALUES (?, ?, ?)
bind => [3 parameters bound]|#]

So this is not safe, because JPA tries store CASE_ID (which is then replaced by ID from Oracle sequence by trigger).

Entity

@Id
@Column(name = "CASE_ID", insertable = false)
private Long caseId;

Create method

public void createCase(final Case caseData) {
    caseData.setCaseId(-1001L);
    em.persist(caseData);
}

JPA log

INSERT INTO CASE (CASE_DATE, INFO) VALUES (?, ?)
bind => [2 parameters bound]|#]

It is good, because the CASE_ID is not part of insert command.

And Update of CASE_ID is not possible because annotation ID:

public void update() {
    Case update = em.find(Case.class, 1023L);
    update.setCaseId(1028L);
}

Exception [EclipseLink-7251] (Eclipse Persistence Services - 2.3.2.v20111125-r10461):
org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [caseId] of class
[com.wordpress.kamiluv.jsfprototype.model.entity.Case] is mapped to a primary
key column in the database. Updates are not allowed.

So now the last version looks as the most safe, right?

like image 515
sasynkamil Avatar asked Apr 12 '12 16:04

sasynkamil


People also ask

What is insertable and updatable in JPA?

The accepted answer conveys the feeling that insertable/updatable attribute has to do with the creation/update of the related entity, whereas the real intention behind these attributes is to prevent insertion/update of the column in the current entity.

Is @column mandatory in JPA?

@Column. Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.

What is @column in JPA?

In this article, we will discuss how to change the column name in the Spring project using JPA. @Column annotation is used for Adding the column the name in the table of a particular MySQL database. Syntax: @Column(name=”DESC”, nullable=false, length=512)

What is nullable false in hibernate?

The @Column(nullable = false) Annotation It's used mainly in the DDL schema metadata generation. This means that if we let Hibernate generate the database schema automatically, it applies the not null constraint to the particular database column.


1 Answers

You are currently saying with your JPA annotations that you have an @Id column that cannot be inserted or updated in any way. You must be able to set the id before inserting or updating, but JPA does not know how to do so. You will need to use the @GeneratedValue annotation on the @Id to tell JPA what strategy to use (one of: TABLE,SEQUENCE,IDENTITY,AUTO) if you do not want the id to be set in your code.

like image 138
bobz32 Avatar answered Sep 30 '22 12:09

bobz32