我已经阅读了许多关于类似错误的帖子,并尝试了所有建议,但我遇到的错误仍未解决。
在spring mvc
应用中使用hibernate
和jpa
且InheritanceType.JOINED
高于MySQL数据库时,我在尝试持久化子类时不断收到以下错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '4' for key 'PRIMARY'
错误始于1
,然后是2
,然后是3
,然后是4
,尽管父表的值已经达到{{} {1}}。这让我怀疑对子类的11
调用在某种程度上看不到父表的主键值。 但是如何解决此问题并解决错误?以下是抛出错误时正在运行的存储库代码:
EntityManager.persist()
接下来是类的注释。请注意,我还尝试过@Override
public void saveRIMOrganization(Organization myorg) throws DataAccessException{
if (myorg.getHppid() == null) {this.em.persist(myorg);}//ERROR THROWN HERE
else {this.em.merge(myorg);}
}
,strategy = "native"
和strategy = "identity"
,但我仍然遇到同样的错误。
strategy = "sequence"
数据库表是由hbm2ddl创建的,所以我需要在java / hibernate级别进行任何必要的更改,但我做了与上述实体对应的三个表的MySQL转储,并且ddl代码是:< / p>
@Entity(name = "rimInfrastructureRoot")
@Table(name = "rim_infrastructure_root")
@Inheritance(strategy = InheritanceType.JOINED)
public class InfrastructureRoot {
@Id
@Column(name="hppid")
@GenericGenerator(name = "generator", strategy = "native")
@GeneratedValue(generator = "generator")
private Long hppid;
public Long getHppid(){return hppid;}//non-compliant, added by jim
public void setHppid(Long i){hppid=i;}//non-compliant, added by jim
//other stuff
}
@Entity(name = "rimEntity")
@Table(name = "rim_entity")
public class rimEntity extends InfrastructureRoot{
//other stuff
}
@Entity(name = "rimOrganization")
@Table(name = "rim_organization")
public class Organization extends rimEntity {
}