我们使用Oracle作为我们的单元测试数据库的生产数据库和HSQLDB。两种环境都使用Hibernate作为JPA提供程序。以下代码在生产中正常工作,但在jUnit下运行时抛出SQL Error: -104, SQLState: 23505 - integrity constraint violation: unique constraint or index violation
。有问题的实体是:
@Entity
@Table(name="TT_TEACHERSECTIONGROUP")
public class TeacherSectionGroup implements TeacherSectionListItem {
@Id
@Column(name="TEACHERSECTIONGROUPID",unique=true,nullable=false)
@GeneratedValue(strategy=GenerationType.AUTO,generator="TT_TEACHERSECTIONGROUPID_SEQUENCE")
@SequenceGenerator(name="TT_TEACHERSECTIONGROUPID_SEQUENCE",sequenceName="TT_TEACHERSECTIONGROUP_SQ")
private Long id;
.
.
.
@OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.DETACH)
@JoinTable(name="TT_TEACHERSECTIONGROUPASSOC",joinColumns={
@JoinColumn(name="TEACHERSECTIONGROUPID",referencedColumnName="TEACHERSECTIONGROUPID")
},inverseJoinColumns={
@JoinColumn(name="USERSDCID",referencedColumnName="USERSDCID"),
@JoinColumn(name="SECTIONSDCID",referencedColumnName="SECTIONSDCID")
})
private List<TeacherSection> sections;
.
.
.
加入的另一端:
@Entity
@Table(name="TT_TEACHERSECTION")
public class TeacherSection implements TeacherSectionListItem {
@EmbeddedId
private TeacherSectionPK key;
.
.
.
使用主键类:
@Embeddable
public class TeacherSectionPK implements Serializable {
@Column(name="SECTIONSDCID",nullable=false)
private Long sectionId;
@Column(name="USERSDCID",nullable=false)
private Long usersDcid;
.
.
.
日志中的(已清理的)输出包含正在发布的这些hibernate语句:
DEBUG org.hibernate.SQL - insert into TT_TEACHERSECTION (DISPLAYNAME, POSITION, SECTIONSDCID, USERSDCID) values (<null>, <null>, 13820, 1)
DEBUG org.hibernate.SQL - insert into TT_TEACHERSECTION (DISPLAYNAME, POSITION, SECTIONSDCID, USERSDCID) values (<null>, <null>, 13821, 1)
DEBUG org.hibernate.SQL - insert into TT_TEACHERSECTION (DISPLAYNAME, POSITION, SECTIONSDCID, USERSDCID) values (<null>, <null>, 13957, 1)
DEBUG org.hibernate.SQL - insert into TT_TEACHERSECTION (DISPLAYNAME, POSITION, SECTIONSDCID, USERSDCID) values (<null>, <null>, 13958, 1)
DEBUG org.hibernate.SQL - insert into TT_TEACHERSECTION (DISPLAYNAME, POSITION, SECTIONSDCID, USERSDCID) values (<null>, <null>, 14234, 1)
DEBUG org.hibernate.SQL - insert into TT_TEACHERSECTION (DISPLAYNAME, POSITION, SECTIONSDCID, USERSDCID) values (<null>, <null>, 14235, 1)
DEBUG org.hibernate.SQL - insert into TT_TEACHERSECTIONGROUP (TEACHERSECTIONGROUPID, DISPLAYNAME, EXPRESSION, ISMODIFIED, NAME, POSITION, USERSDCID) values (default, <null>, 'Course(91642)', false, 'AP Chemistry', 0, 1)
DEBUG org.hibernate.SQL - insert into TT_TEACHERSECTIONGROUP (TEACHERSECTIONGROUPID, DISPLAYNAME, EXPRESSION, ISMODIFIED, NAME, POSITION, USERSDCID) values (default, <null>, 'Course(723)', false, 'Gen Chemistry', 1, 1)
DEBUG org.hibernate.SQL - insert into TT_TEACHERSECTIONGROUP (TEACHERSECTIONGROUPID, DISPLAYNAME, EXPRESSION, ISMODIFIED, NAME, POSITION, USERSDCID) values (default, <null>, 'Expression(3(A-B))', false, '3(A-B)', 2, 1)
DEBUG org.hibernate.SQL - insert into TT_TEACHERSECTIONGROUPASSOC (TEACHERSECTIONGROUPID, SECTIONSDCID, USERSDCID) values (1, 13820, 1)
DEBUG org.hibernate.SQL - insert into TT_TEACHERSECTIONGROUPASSOC (TEACHERSECTIONGROUPID, SECTIONSDCID, USERSDCID) values (1, 13821, 1)
DEBUG org.hibernate.SQL - insert into TT_TEACHERSECTIONGROUPASSOC (TEACHERSECTIONGROUPID, SECTIONSDCID, USERSDCID) values (2, 13957, 1)
DEBUG org.hibernate.SQL - insert into TT_TEACHERSECTIONGROUPASSOC (TEACHERSECTIONGROUPID, SECTIONSDCID, USERSDCID) values (2, 13958, 1)
DEBUG org.hibernate.SQL - insert into TT_TEACHERSECTIONGROUPASSOC (TEACHERSECTIONGROUPID, SECTIONSDCID, USERSDCID) values (2, 14234, 1)
DEBUG org.hibernate.SQL - insert into TT_TEACHERSECTIONGROUPASSOC (TEACHERSECTIONGROUPID, SECTIONSDCID, USERSDCID) values (2, 14235, 1)
DEBUG org.hibernate.SQL - insert into TT_TEACHERSECTIONGROUPASSOC (TEACHERSECTIONGROUPID, SECTIONSDCID, USERSDCID) values (3, 13957, 1)
WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: -104, SQLState: 23505
ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - integrity constraint violation: unique constraint or index violation; SYS_CT_10323 table: TT_TEACHERSECTIONGROUPASSOC
看起来HSQLDB只使用反向连接列作为JoinTable的主键而不是所有列,这是有意义的(正如我已经看到的那样在管间记录)
我是否缺少为HSQLDB指定哪些字段应包含在主键中的机制?由于这段代码实际上在Oracle中正常工作(即生产),我不愿重写它只是为了满足单元测试,所以我希望有一个答案在两种环境中同样有效。感谢。