我遇到了非常简单的EntityManager merge()方法的问题。 如果DB中不存在条目记录(基于PK),则应插入,否则更新。但在我的情况下,merge()更新了记录,但是当需要插入而不是插入实体对象的值时,尝试在所有字段中插入空值。所以我收到以下错误:
SQLIntegrityConstraintViolationException:ORA-01400:无法插入NULL(“REFDATA”。“CLIENT”。“ID”)
这是我的存储库和实体代码:
@Repository
public class BenDAOImpl implements BenDAO {
@Autowired
EntityManager em;
@Override
@Transactional
public void saveBen(Ben ben) {
em.merge(ben);
}
}
//////////////////////////////////////////////
@Entity
@Table(name = "CLIENT")
public class Ben implements Serializable{
@Id
public String id;
@Id
public String ownerId;
public String country;
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
Ben other = (Ben) obj;
return id.equals(other.id) && ownerId.equals(other.ownerId);
}
}
如果我用persist替换merge,则插入记录时没有错误。
为什么?
答案 0 :(得分:0)
ORA-01400表示你的id为null。尝试将idGenerator设置为id字段。 对不起,我没有立即注意到你有两个ID。你必须做那样的事情。因为它是复合键
hover