我有我的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規範的兩種不同實現。 –