我有我的EclipseLink的一对一的关系出了问题,关系是用户和代理之间,且误差JPA字段没有默认值,以一对一的关系
异常[EclipseLink-4002](Eclipse持久性服务 - 2.5.0.v20130507-3faac2b):org.eclipse.persistence.exceptions.DatabaseException内部 异常:java.sql.SQLException:字段'userId'没有 默认值错误代码:1364调用:INSERT INTO AGENT(ADDRESS,NOM, PRENOM)VALUE(?,?,?)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("[email protected]");
user.setPassword("[email protected]");
agent.setUser(user);
agentService.add(agent);
你没有使用Hibernate。您正在使用EclipseLink。 –
但导入全部是导入javax.persistence。*; –
是的,这是标准的JPA软件包。 Hibernate和EclipseLink是JPA规范的两种不同实现。 –