-1

I have a problem with my one to one relationship in ECLIPSELINK, the relation is between a User and an Agent, and the error is

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLException: Field 'userId' doesn't have a default value Error Code: 1364 Call: INSERT INTO AGENT (ADDRESS, NOM, PRENOM) VALUES (?, ?, ?) bind => [3 parameters bound]

class diagram

database implementation

and this is my JPA Code

@Entity
@NamedQuery(name="User.findAll", query="SELECT u FROM User u")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private int active;
    private String email;
    private String password;
    //bi-directional one-to-one association to Agent
    @OneToOne(mappedBy="user",cascade=CascadeType.PERSIST)
    private Agent agent;
    //....


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

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    private String address;

    private String nom;

    private String prenom;

    //bi-directional one-to-one association to User
    @OneToOne(cascade=CascadeType.PERSIST)
    @PrimaryKeyJoinColumn(name="userId")
    private User user;
//...
}

//the code that contains the error
        Agent agent = new Agent();
        agent.setAddress("addresse 1");
        agent.setNom("nom1");
        agent.setPrenom("prenom 1");
        User user = new User();
        user.setActive(1);
        user.setEmail("[email protected]");
        user.setPassword("p@ssword");
        agent.setUser(user);
        agentService.add(agent);
5
  • You're not using Hibernate. You're using EclipseLink. Commented Dec 28, 2016 at 22:39
  • but the imports are all import javax.persistence.*; Commented Dec 28, 2016 at 22:41
  • Yes, that's the standard JPA package. Hibernate and EclipseLink are two different implementations of the JPA specifications. Commented Dec 28, 2016 at 22:43
  • how can i fix the error? Commented Dec 28, 2016 at 22:46
  • Don't you need a agentService.add(user) as well or userService.add(user)? Commented Dec 28, 2016 at 23:06

2 Answers 2

2

By having your "id" field as "IDENTITY" strategy, in the datastore this column MUST be something like AUTO_INCREMENT (MySQL) or SERIAL, or IDENTITY. That is what IDENTITY strategy has as a prerequisite (and if you had let the JPA provider generate the schema would have got by default).

If it isn't then either change the column type to be like this, or change the strategy to be something that generates the value at runtime (not in the datastore).

Sign up to request clarification or add additional context in comments.

Comments

1

I have already known this kind of problem with EclipseLink when I used PrimaryKeyJoinColumn.

In the Agent entity, you define id as PK :

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;

but you also define another field that bears the PK(userId which is also a foreign key on User table) :

//bi-directional one-to-one association to User
@OneToOne(cascade=CascadeType.PERSIST)
@PrimaryKeyJoinColumn(name="userId")
private User user;


I am not sure that is your target when I see you database diagram.

If I don't see right and that you really want to do that, you could try to use something as an Embeddable object to bear the required fields of the PK.
if you have always the exception, you could bypass the problem by persisting first the user alone, then to set it to the agent and persist the agent.

If you don't need to have this composite key in Agent, replace @PrimaryKeyJoinColumn by @JoinColumn :

Instead of that :

//bi-directional one-to-one association to User
@OneToOne(cascade=CascadeType.PERSIST)
@PrimaryKeyJoinColumn(name="userId")
private User user;

Do that :

//bi-directional one-to-one association to User
@OneToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="userId")
private User user;

2 Comments

i have replaced the @PrimaryKeyJoinColumn(name="userId") by @JoinColumn(name="userId") and the error now is Multiple writable mappings exist for the field userId , and it doesn't deploy anymore
In your actual mapping of Agent, userId is mapped once. If you have another mapping for it, you should edit your post and show all relevant information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.