在spring数据jdbc中使用值对象作为id-无法插入

ztyzrc3y  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(441)

我试图在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),那么插入就可以工作了

6mzjoqzu

6mzjoqzu1#

@EmbeddedId   //have not found onEmpty parameter in any documentation 
  private PartyId partyId;
@Embeddable
 public class PartyId

覆盖 equals 以及 hashcode 上的方法 Party class 考虑 partyId 现场。
PartyId class 和覆盖 equals 以及 hashcode 方法来考虑类的所有id字段 PartyId

x7yiwoj4

x7yiwoj42#

springdatajdbc还不支持嵌入式id。但是自从你 PartyId 似乎只包含一个值,您可以为它注册自定义转换并获得所需的结果。
下面是一个注册转换的示例。
创建如下配置类

@Configuration
static class ConversionConfigurationApplication extends AbstractJdbcConfiguration {

    @Override
    @Bean
    public JdbcCustomConversions jdbcCustomConversions() {
        return new JdbcCustomConversions(asList(PartyIdToString.INSTANCE, StringToPartyId.INSTANCE));
    }

    @WritingConverter
    enum PartyIdToString implements Converter<PartyId, String> {

        INSTANCE;

        @Override
        public String convert(PartyId source) {
            return source.partyId;
        }
    }

    @ReadingConverter
    enum StringToPartyId implements Converter<String, PartyId> {

        INSTANCE;

        @Override
        public PartyId convert(String source) {
            return new PartyId(source);
        }
    }

}

相关问题