Java Hibernate@ManyToMany——联接表不';没有人居住

Java Hibernate@ManyToMany——联接表不';没有人居住,java,mysql,hibernate,many-to-many,Java,Mysql,Hibernate,Many To Many,我正在尝试实现@manytomy关系,但没有填充我的联接表。我的应用程序将一组竞争对象建模为竞争对象。有一群竞争者。在该组中,每个竞争对手与其他竞争对手进行配对,形成竞争。因此,每次比赛都可以有一个竞争对手对象列表。每个参赛者可以参加多场比赛 实体对象 竞争是这样的: @Entity @Table(name = "competition") public class Competition implements java.io.Serializable { @Id @Gener

我正在尝试实现@manytomy关系,但没有填充我的联接表。我的应用程序将一组竞争对象建模为竞争对象。有一群竞争者。在该组中,每个竞争对手与其他竞争对手进行配对,形成竞争。因此,每次比赛都可以有一个竞争对手对象列表。每个参赛者可以参加多场比赛

实体对象

竞争是这样的:

@Entity
@Table(name = "competition")
public class Competition implements java.io.Serializable {

    @Id
    @GeneratedValue
    private int competitionId;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "competition_competitors", joinColumns = @JoinColumn(name = "competitionId"), inverseJoinColumns = @JoinColumn(name = "competitorId"))
    private Set<Competitor> competitors = new TreeSet<Competitor>();

            . . .
@Entity
@Table(name = "competitor")
public class Competitor implements Comparable<Competitor>, java.io.Serializable {

    @Id
    @GeneratedValue
    private int competitorId;

    @ManyToMany(mappedBy = "competitors", cascade = CascadeType.ALL)
    private Set<Competition> competitions = new TreeSet<Competition>();

            . . .
竞争DAO方法

@Override
@Transactional(readOnly = false)
public Competition createCompetition(League league, Competitor competitor1, Competitor competitor2) {
    Competition competition = getCompetitionDAO().create(league);
    competition.add(competitor1);
    competition.add(competitor2);
    this.competitionDao.saveOrUpdate(competition);
    return competition;
}
@Override
@Transactional(readOnly = false)
public void saveOrUpdate(Competition competition) {
    if (competition != null) {
        Session session = getCurrentSession();
        try {
            session.saveOrUpdate(competition);
        } catch (Exception exception) {
            throw new AppException(exception);
        }
    }
}
应用程序代码

这段代码应该创建一个竞争并保持它。它以前是这样做的,但在这条线路的某个地方我把它弄坏了,它不再保存任何东西到联接表{competition_competitors}

@Transactional(readOnly = false)
private void createLeague(LeagueData leagueData) {

    User user = createAccount( ... );
    League league = createLeague(user, leagueData.getLeagueName());

    for (int index = 0; index < leagueData.getCompetitorNames().length; index++) {
        Competitor competitor = this.serviceProvider.getCompetitorService().find(league, leagueData.getCompetitorNames()[index]);
        if (competitor == null) {
            competitor = createCompetitor(league, leagueData.getCompetitorNames()[index], leagueData.getCompetitorDescription()[index]);
        }
        this.serviceProvider.getCompetitionService().createCompetition(league, competitor);
    }
}
@Transactional(readOnly=false)
私人联盟(LeagueData LeagueData){
User=createAccount(…);
League League=createLeague(用户,leagueData.getLeagueName());
对于(int index=0;index

问题:关于为什么在应用程序运行时不填充联接表,您有什么想法吗?应用程序代码运行时没有错误。我不明白为什么单元测试可以正常工作并正确填充联接表,但类似的应用程序代码却不能。如果你看不到MySQL Workbench中出现的值,但你的单元测试通过了,那是因为Spring在单元测试完成后自动回滚事务。因此,如果您能够在事务提交时立即刷新MySQL Workbench,那么在事务回滚和值丢失之前,您将看到大约0.2毫秒的值


您的联接表正在工作,否则您的断言将失败。

Spring将在测试用例完成后回滚事务。要避免回滚行为,请在测试类上使用
@TransactionConfiguration(defaultRollback=false)
注释。

您的意思是在MySQL Workbench中看不到联接表中的值,还是您的断言在单元测试中失败。单元测试工作正常,我设置了“defaultRollback=false”,并填充联接表。我提供了代码以查看如何使用该类。该应用程序不会填充联接表。因此,当您使用该应用程序时,即使该应用程序运行fineNo,也无法在联接表中看到任何内容。应用程序无法工作,因为未填充联接表。当我加载竞争时,我无法获取相关的竞争对手。但是,competition表和competitor表已正确填充。您的日志记录中是否存在任何异常?您可以发布执行对象保存的服务层代码吗单元测试工作正常,我设置了“defaultRollback=false”,并填充了联接表。我提供了代码以查看如何使用该类。是应用程序没有填充联接表。单元测试工作正常,我设置了'defaultRollback=false',联接表被填充。我提供了代码以查看如何使用该类。是应用程序没有填充联接表。