我试图实现@ManyToMany关系,但我的联接表未被填充。我的应用程序为竞争对手的对象建模了一堆竞争对象。有一群竞争对手。在该组中,每个竞争者彼此匹配以形成竞赛。因此每个比赛都可以有一个竞争对手的对象列表。每个参赛者都可参加多场比赛。
实体对象
比赛看起来像这样:
@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>();
. . .
竞争服务方法
@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;
}
CompetitionDAO方法
@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);
}
}
问题:关于为什么在应用程序运行时没有填充连接表的任何想法?应用程序代码运行时没有错误。我不明白为什么单元测试工作正常并填充连接表,但类似的应用程序代码没有。
答案 0 :(得分:1)
如果你看不到MySQL Workbench中出现的值,但你的单元测试正在通过,那是因为Spring在单元测试完成后会自动回滚事务。因此,如果您可以在提交事务的确切时刻立即刷新MySQL Workbench,那么您将在事务回滚之前看到您的值约0.2毫秒并且值丢失。
您的联接表正在运行,否则您的断言将失败。
答案 1 :(得分:0)
Spring在测试用例结束后回滚事务。要避免回滚行为,请在测试类上使用@TransactionConfiguration(defaultRollback = false)
注释。