如何更新一对多关系中的项目

时间:2016-11-08 20:29:01

标签: java spring hibernate jpa spring-boot

我正在尝试更新一对多关系,但要获得

  

插入sw_standard_standard(sw_standard_id,standard_id)值   (?,?)[23505-192]];嵌套异常是   org.hibernate.exception.ConstraintViolationException

这是我的代码: SwStandard

@Entity
public class SwStandard implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id;

    @OneToOne
    private DeviceType deviceType;

    @ElementCollection
    @OneToMany
    private List<Image> standard;
    @ElementCollection
    @OneToMany
    private List<Image> limited;
    @ElementCollection
    @OneToMany
    private List<Image> exception;

    public SwStandard() {}

    public SwStandard(DeviceType deviceType, List<Image> standard, List<Image> limited, List<Image> exception) {
        this.deviceType = deviceType;
        this.standard = standard;
        this.limited = limited;
        this.exception = exception;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public DeviceType getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(DeviceType deviceType) {
        this.deviceType = deviceType;
    }

    public List<Image> getStandard() {
        return standard;
    }

    public void setStandard(List<Image> standard) {
        this.standard = standard;
    }

    public List<Image> getLimited() {
        return limited;
    }

    public void setLimited(List<Image> limited) {
        this.limited = limited;
    }

    public List<Image> getException() {
        return exception;
    }

    public void setException(List<Image> exception) {
        this.exception = exception;
    }
}

图像

@Entity
public class Image implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id;
    private String name;

    public Image() {}

    public Image(String name) {
        this.name = name;
    }

    public Image(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

更新代码:

@RequestMapping(value = "/swstandard/update", method = RequestMethod.POST)
     public SwStandard updateSwStandard(@RequestBody SwStandard request) {
        return swService.save(request);
}

SwService:

@Autowired
private SwStandardRepository swService;

SwStandardRepository:

public interface SwStandardRepository extends CrudRepository<SwStandard, Long> {
    SwStandard findByDeviceTypeId(Long id);
}

首先我用一个标准项保存SwStandard。然后我保存想用其他东西替换标准项目。当我这样做时,它返回上面的错误。

修改 Alan Hay帮助部分帮助。 现在我无法在SwStandard中保存相同的image,例如这里是一个SwStandard I&#39; ll save:

{
    "id": 1,
    "deviceType": { "id": 1 },
    "standard": [{"id": 1}],
    "limited": [],
    "exception": []
}

这是另一个SwStandard I&#39; ll保存,但会因上述错误而失败:

{
    "id": 2,
    "deviceType": { "id": 2 },
    "standard": [{"id": 1}],
    "limited": [],
    "exception": []
}

正如您所看到的,两个SwStandards都有相同的标准项目。

这是日志输出:

2016-11-08 16:19:12.060  INFO 15668 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-11-08 16:19:12.060  INFO 15668 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2016-11-08 16:19:12.092  INFO 15668 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 31 ms
2016-11-08 16:19:12.255  WARN 15668 --- [nio-8080-exec-1] org.hibernate.orm.deprecation            : HHH90000015: Found use of deprecated [org.hibernate.id.MultipleHiLoPerTableGenerator] table-based id generator; use org.hibernate.id.enhanced.TableGenerator instead.  See Hibernate Domain Model Mapping Guide for details.
2016-11-08 16:19:12.255 DEBUG 15668 --- [nio-8080-exec-1] org.hibernate.SQL                        : select sequence_next_hi_value from hibernate_sequences where sequence_name = 'sw_standard' for update
2016-11-08 16:19:12.256 DEBUG 15668 --- [nio-8080-exec-1] org.hibernate.SQL                        : insert into hibernate_sequences(sequence_name, sequence_next_hi_value) values('sw_standard', ?)
2016-11-08 16:19:12.258 DEBUG 15668 --- [nio-8080-exec-1] org.hibernate.SQL                        : update hibernate_sequences set sequence_next_hi_value = ? where sequence_next_hi_value = ? and sequence_name = 'sw_standard'
2016-11-08 16:19:12.262 DEBUG 15668 --- [nio-8080-exec-1] org.hibernate.SQL                        : insert into sw_standard (device_type_id, id) values (?, ?)
2016-11-08 16:19:12.262 TRACE 15668 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [1]
2016-11-08 16:19:12.262 TRACE 15668 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [BIGINT] - [1]
2016-11-08 16:19:12.264 DEBUG 15668 --- [nio-8080-exec-1] org.hibernate.SQL                        : insert into sw_standard_standard (sw_standard_id, standard_id) values (?, ?)
2016-11-08 16:19:12.270 TRACE 15668 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [1]
2016-11-08 16:19:12.271 TRACE 15668 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [BIGINT] - [1]
2016-11-08 16:19:22.162  WARN 15668 --- [nio-8080-exec-2] org.hibernate.orm.deprecation            : HHH90000015: Found use of deprecated [org.hibernate.id.MultipleHiLoPerTableGenerator] table-based id generator; use org.hibernate.id.enhanced.TableGenerator instead.  See Hibernate Domain Model Mapping Guide for details.
2016-11-08 16:19:22.163 DEBUG 15668 --- [nio-8080-exec-2] org.hibernate.SQL                        : insert into sw_standard (device_type_id, id) values (?, ?)
2016-11-08 16:19:22.164 TRACE 15668 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [2]
2016-11-08 16:19:22.164 TRACE 15668 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [BIGINT] - [2]
2016-11-08 16:19:22.165 DEBUG 15668 --- [nio-8080-exec-2] org.hibernate.SQL                        : insert into sw_standard_standard (sw_standard_id, standard_id) values (?, ?)
2016-11-08 16:19:22.166 TRACE 15668 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [2]
2016-11-08 16:19:22.166 TRACE 15668 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [BIGINT] - [1]
2016-11-08 16:19:22.169  WARN 15668 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 23505, SQLState: 23505
2016-11-08 16:19:22.169 ERROR 15668 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : Unique index or primary key violation: "UK_QM6PF2T2PB3DLYYLV44TQL40J_INDEX_2 ON PUBLIC.SW_STANDARD_STANDARD(STANDARD_ID) VALUES (1, 1)"; SQL statement:
insert into sw_standard_standard (sw_standard_id, standard_id) values (?, ?) [23505-192]
2016-11-08 16:19:22.172  INFO 15668 --- [nio-8080-exec-2] o.h.e.j.b.internal.AbstractBatchImpl     : HHH000010: On release of batch it still contained JDBC statements
2016-11-08 16:19:22.194 ERROR 15668 --- [nio-8080-exec-2] 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 ["UK_QM6PF2T2PB3DLYYLV44TQL40J_INDEX_2 ON PUBLIC.SW_STANDARD_STANDARD(STANDARD_ID) VALUES (1, 1)"; SQL statement:
insert into sw_standard_standard (sw_standard_id, standard_id) values (?, ?) [23505-192]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

0 个答案:

没有答案