【发布时间】:2016-12-28 22:37:04
【问题描述】:
我在 ECLIPSELINK 中的一对一关系有问题,关系是用户和代理之间的关系,错误是
异常 [EclipseLink-4002](Eclipse 持久性服务 - 2.5.0.v20130507-3faac2b):org.eclipse.persistence.exceptions.DatabaseException 内部 异常:java.sql.SQLException:字段 'userId' 没有 默认值 错误代码:1364 调用:INSERT INTO AGENT (ADDRESS, NOM, PRENOM) VALUES (?, ?, ?) bind => [3 个参数绑定]
这是我的 JPA 代码
@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("test@mail.local");
user.setPassword("p@ssword");
agent.setUser(user);
agentService.add(agent);
【问题讨论】:
-
你没有使用 Hibernate。您正在使用 EclipseLink。
-
但是导入的都是import javax.persistence.*;
-
是的,这是标准的 JPA 包。 Hibernate 和 EclipseLink 是 JPA 规范的两种不同实现。
-
我该如何解决这个错误?
-
你不需要agentService.add(user)还是userService.add(user)?
标签: java jpa jakarta-ee eclipselink