多对多映射与额外的列持续存在问题

Many to many mapping with extra column persisting troubles

我有带有额外列的多对多映射。这里有一些带有映射的代码。

集团实体

@Id
@GeneratedValue
private Long id;

@OneToMany(mappedBy = "group", cascade = CascadeType.ALL)
private Set<GroupBrigdeEntity> groupExchanges;

GroupBrigdeEntity

@Id
@ManyToOne
@JoinColumn(name = "group_id")
private GroupEntity group;

@Id
@ManyToOne
@JoinColumn(name = "exchange_code")
private ExchangeEntity exchange;

@Column(name = "enabled")
private Boolean enabled;

交换实体

@Id
@Column(nullable = false)
private String code;

@OneToMany(mappedBy = "exchange")
private Set<GroupBrigdeEntity> exchangeGroups;

我的目标是获得具有给定 id 的组并向其中添加一些新的交流。我是这样做的:

GroupEntity groupEntity = groupRepository.findOne(groupExchangeSettings.getGroupId());
    for (GroupExchange exchange : groupExchangeSettings.getExchanges()) {
        GroupBridgeEntity groupBridge = new GroupExchangeBunchEntity();
        groupBridge.setEnabled(exchange.getEnabled());
        groupBridge.setExchange(exchangeRepository.findByCode(exchange.getExchangeCode()));
        groupBridge.setGroup(groupEntity);
        groupEntity.getGroupExchanges().add(groupExchangeBunch);
    }
    groupRepository.save(groupEntity);

当我 运行 这段代码出现异常时

2016-09-05 15:27:41.031  WARN 25232 --- [nio-8090-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1048, SQLState: 23000
2016-09-05 15:27:41.032 ERROR 25232 --- [nio-8090-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : Column 'group_id' cannot be null
2016-09-05 15:27:41.033  INFO 25232 --- [nio-8090-exec-3] o.h.e.j.b.internal.AbstractBatchImpl     : HHH000010: On release of batch it still contained JDBC statements
2016-09-05 15:27:41.034  WARN 25232 --- [nio-8090-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Warning Code: 1048, SQLState: 23000
2016-09-05 15:27:41.034  WARN 25232 --- [nio-8090-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : Column 'group_id' cannot be null
2016-09-05 15:27:41.034  WARN 25232 --- [nio-8090-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Warning Code: 1048, SQLState: 23000
2016-09-05 15:27:41.035  WARN 25232 --- [nio-8090-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : Column 'exchange_code' cannot be null
2016-09-05 15:27:41.049 ERROR 25232 --- [nio-8090-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'group_id' cannot be null

我猜我的 GroupBrigdeEntity 没有正确持久化,因为它的属性在级联持久化时为空。有人能帮我吗? P.S希望你能原谅我的英语不好。

你有一个中间 class 有多个 @Id 注释,因此对象的身份需要由 @IdClass 注释定义。对于 JPA 提供者要找到此 class 的对象,它需要一个表示该对象的单一身份 (PK) 对象,因此这就是为什么您需要一个 id-class - 同样的事情是如果您想找到它,请传递给 em.find。这在 JPA 规范中,JPA documentation.