我试图在spring数据jdbc中使用partyid(值对象)作为id。我在partyid中设置了这个值(您可以在下面的第一个屏幕截图中看到),但是在插入时会出现以下错误。
调试原因屏幕截图
堆栈跟踪
Caused by: org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL
[INSERT INTO "party" ("currency_uom_id", "description", "party_id", "party_type_id", "status_id", "version") VALUES (?, ?, ?, ?, ?, ?)]; ERROR: null value in column "party_id" violates not-null constraint
Detail: Failing row contains (null, null, Organisation, USD, Enable, 1).; nested exception is org.postgresql.util.PSQLException: ERROR: null value in column "party_id" violates not-null constraint
Detail: Failing row contains (null, null, Organisation, USD, Enable, 1).
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:251) ~[spring-jdbc-5.3.5.jar:5.3.5]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70) ~[spring-jdbc-5.3.5.jar:5.3.5]
at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1541) ~[spring-jdbc-5.3.5.jar:5.3.5]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:667) ~[spring-jdbc-5.3.5.jar:5.3.5]
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:991) ~[spring-jdbc-5.3.5.jar:5.3.5]
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:356) ~[spring-jdbc-5.3.5.jar:5.3.5]
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:340) ~[spring-jdbc-5.3.5.jar:5.3.5]
at org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy.insert(DefaultDataAccessStrategy.java:138) ~[spring-data-jdbc-2.1.6.jar:2.1.6]
at org.springframework.data.jdbc.core.JdbcAggregateChangeExecutionContext.executeInsertRoot(JdbcAggregateChangeExecutionContext.java:90) ~[spring-data-jdbc-2.1.6.jar:2.1.6]
at org.springframework.data.jdbc.core.AggregateChangeExecutor.execute(AggregateChangeExecutor.java:66) ~[spring-data-jdbc-2.1.6.jar:2.1.6]
... 96 common frames omitted
错误是partyid为null,这违反了db约束。
作为聚合根的party类
@AccessType(AccessType.Type.PROPERTY)
public class Party implements AggregateRoot {
//Business key Id;
@Id//Use by spring data jdbc
@Embedded(onEmpty = USE_NULL)
private PartyId partyId; // private String partyId - this is working (removing @Embedded also)
private String description;
@Embedded(onEmpty = USE_NULL)
private PartyTypeId partyTypeId;
@Embedded(onEmpty = USE_NULL)
private CurrencyUomId currencyUomId;
@Embedded(onEmpty = USE_NULL)
private StatusId statusId;
@Version
private long version;
/*private List<PartyAttributes> partyAttributes = new ArrayList<>();
private List<PartyCarrierAccount> partyCarrierAccounts = new ArrayList<>();*/
//Only package level access
public Party(PartyId partyId,String description,PartyTypeId partyTypeId,CurrencyUomId currencyUomId,StatusId statusId,List<PartyAttributes> partyAttributes,List<PartyCarrierAccount> partyCarrierAccounts){
this.partyId = partyId;
this.partyTypeId = partyTypeId;
this.currencyUomId = currencyUomId;
this.statusId = statusId;
}
如果我将partyid设为string(party类中的私有字符串partyid,显然从partyid中删除@embedded annotation),那么插入就可以工作了
2条答案
按热度按时间6mzjoqzu1#
覆盖
equals
以及hashcode
上的方法Party class
考虑partyId
现场。去
PartyId class
和覆盖equals
以及hashcode
方法来考虑类的所有id字段PartyId
x7yiwoj42#
springdatajdbc还不支持嵌入式id。但是自从你
PartyId
似乎只包含一个值,您可以为它注册自定义转换并获得所需的结果。下面是一个注册转换的示例。
创建如下配置类