Hibernate静默插入失败,FK为空,未引发异常

Hibernate静默插入失败,FK为空,未引发异常,hibernate,Hibernate,我在Hibernate上遇到了一些问题,它没有在我期望的时候抛出异常。简言之,我有一个实体映射到一个具有所需FK(Hibernate生成的表)的表;如果在Java中正确指定了FK,那么一切都会顺利进行。但是,如果省略FK,则实体不会保存到数据库中。很好--但我在会话.save()时间,甚至在事务.commit()时间都没有收到异常;我只有在尝试加载实体时才意识到问题。这使得调试成为一场噩梦。当然Hibernate最晚应该在我提交时发出某种错误 我不知道错误可能来自哪里,所以我不太确定如何包含适当

我在Hibernate上遇到了一些问题,它没有在我期望的时候抛出异常。简言之,我有一个实体映射到一个具有所需FK(Hibernate生成的表)的表;如果在Java中正确指定了FK,那么一切都会顺利进行。但是,如果省略FK,则实体不会保存到数据库中。很好--但我在
会话.save()
时间,甚至在
事务.commit()
时间都没有收到异常;我只有在尝试加载实体时才意识到问题。这使得调试成为一场噩梦。当然Hibernate最晚应该在我提交时发出某种错误

我不知道错误可能来自哪里,所以我不太确定如何包含适当的特定信息。欢迎任何提示

伪代码:

try {
    tx = session.beginTransaction();

    MyEntity me = new MyEntity();
    me.setName("Foo");
    // Omitting to call me.setSomeFK(someOtherEntity);
    session.save(me);

    tx.commit();
    System.out.println("Everything is OK");
}
catch (Exception e) {
    tx.rollback();
}
可悲的是,它认为“
一切正常”
”。一切都不好


实体代码(示例):

不幸的是,我不能展示整件事,但是

GenericParticipantEntity.java

@Entity
@Table
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name = "participant_type",
    discriminatorType = DiscriminatorType.STRING,
    length = 16
)
abstract public class GenericParticipantEntity {
    private long participantId;
    private StatEventEntity event;
    private SeasonEventEntity seasonEvent;
    // A bunch of other fields

    @Id
    @Column(name = "participantId")
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long getParticipantId() {
        return participantId;
    }
    public void setParticipantId(long participantId) {
        this.participantId = participantId;
    }

    @ManyToOne(targetEntity = StatEventEntity.class, optional = false)
    @JoinColumn(name = "stat_event_id")
    public StatEventEntity getEvent() {
        return event;
    }
    public void setEvent(StatEventEntity event) {
        this.event = event;
    }

    @ManyToOne
    @JoinColumn(name = "season_event_id", nullable = false)
    public SubSeasonEventEntity getSeasonEvent() {
        return seasonEvent;
    }
    public void setSeasonEvent(SubSeasonEventEntity seasonEvent) {
        this.seasonEvent = seasonEvent;
    }
}
SpecificParticipant.java
(这个确实不包含代码):

用法:

Transaction tx;
Long id = null;
try {
    tx = session.beginTransaction();
    SpecificParticipant somePlayer = new SpecificParticipant();
    somePlayer.setEvent(someEvent);
    id = session.save(somePlayer);
    tx.commit();
}
catch (Throwable e) {
    if (tx != null) tx.rollback();
    throw e;
}
请注意,我忘了设置
seasureevent
属性,因此
INSERT
查询最终会失败,这是应该的;那是我的错误。让我困惑的是Hibernate在这里也不例外;相反,当我尝试基于
id
加载对象时,我发现了问题,因为数据库中没有要加载的内容


SQL完全由Hibernate生成,因此,如果DDL中有错误,那么肯定是由于Java代码中的错误造成的。

数据库中是否有
notnull
约束?如果没有,那么添加一个。我确实有一个
notnull
约束——可能这就是数据最终无法正确保存的原因。显示实体代码和用于创建表的SQL代码。除非您将会话的刷新模式设置为手动,否则我不认为这是可能的。您是否看到在打开SQL日志记录时发出insert SQL?如果在提交之前调用session.flush(),会发生什么?有趣的是,当我从一个临时MySQL开发环境更改为我们的生产MS SQL Server环境时,问题就消失了(即,当我希望看到错误时,它现在会给出错误)。奇怪的是,我的MySQL表都是InnoDB,应该支持FK检查。所以现在我不能重现这个问题,除非我还原环境……数据库中是否有
notnull
约束?如果没有,那么添加一个。我确实有一个
notnull
约束——可能这就是数据最终无法正确保存的原因。显示实体代码和用于创建表的SQL代码。除非您将会话的刷新模式设置为手动,否则我不认为这是可能的。您是否看到在打开SQL日志记录时发出insert SQL?如果在提交之前调用session.flush(),会发生什么?有趣的是,当我从一个临时MySQL开发环境更改为我们的生产MS SQL Server环境时,问题就消失了(即,当我希望看到错误时,它现在会给出错误)。奇怪的是,我的MySQL表都是InnoDB,应该支持FK检查。所以现在我不能重现这个问题,除非,大概,我还原环境。。。
Transaction tx;
Long id = null;
try {
    tx = session.beginTransaction();
    SpecificParticipant somePlayer = new SpecificParticipant();
    somePlayer.setEvent(someEvent);
    id = session.save(somePlayer);
    tx.commit();
}
catch (Throwable e) {
    if (tx != null) tx.rollback();
    throw e;
}