Question

I have a database with a table with 2 PK.

I have developed the JPA classes

@Entity
@NamedQuery(name="Drive.findAll", query="SELECT d FROM Drive d")
public class Drive implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private DrivePK id;

    @ManyToOne
    @JoinColumn(name="id_content")
    private Content content;

    @ManyToOne
    @JoinColumn(name="id_user")
    private User user;

    public Drive() {
    }

    public Drive(Content content, User user) {
        this.content = content;
        this.user = user;
        this.id = new DrivePK(user.getId(), content.getId());
    }

    // + getters/setters user + content + drivePK
}

and

@Embeddable
public class DrivePK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(name="id_user", insertable=false, updatable=false)
    private int idUser;

    @Column(name="id_content", insertable=false, updatable=false)
    private int idContent;

    public DrivePK() {
    }

    public DrivePK(int idUser, int idContent) {
        this.idUser = idUser;
        this.idContent = idContent;
    }

    // + getters/setters idUser and idContent
}

When I try to persist a drive object like this

Drive drive = new Drive(content, user);
em.create(drive); //after injection

I obtain this error

Avertissement: Unexpected exception from beforeCompletion; transaction will roll back
<openjpa-2.3.0-nonfinal-1540826-r422266:1542644 fatal general error> org.apache.openjpa.persistence.PersistenceException: The transaction has been rolled back. See the nested exceptions for details on the errors that occurred.
    at org.apache.openjpa.kernel.BrokerImpl.newFlushException(BrokerImpl.java:2370)
    ...
Caused by: <openjpa-2.3.0-nonfinal-1540826-r422266:1542644 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: Field "jpa.Drive.id" of "jpa.Drive@85094f" can not be set to "jpa.DrivePK@4bbe" value.

I understand the error is linked with the DrivePK but I don't know what is not correct.

Was it helpful?

Solution

I am pretty sure the problem is caused by the insertable=false attribute on your PK columns -- you're trying to insert a value that you said was non-insertable.

I believe the the updatable=false is okay (you don't want PK fields to be updated), but if it still doesn't work try removing that as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top