gpt4 book ai didi

java - Hibernate完整性约束冲突: NOT NULL check constraint: For onetoOne Mapping using spring boot crud

转载 作者:行者123 更新时间:2023-11-30 05:54:19 27 4
gpt4 key购买 nike

我正在尝试在两个表之间创建onetoone映射,其中父键主键也充当子表的主键。在尝试保存父级时,我收到以下错误。

请找到下面的控制台日志、模型类和服务类。有人可以帮助解决该错误吗?

主要是想使用 crud 存储库将订单 id 从订单类转移到补偿下的订单 id。

父类:

    package com.newModel;
import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="ORDERS")
public class Order implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(name="ORDER_ID")
private String orderId;

@Column(name="ACCESS_ID")
private String accessId;

@OneToOne(cascade=CascadeType.ALL,mappedBy="order",fetch=FetchType.EAGER)
private Compensation compensation;

//getters & setters
}

子类:

package com.newModel;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapsId;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;



@Entity
@Table(name="COMPENSATION")
@NamedQuery(name="Compensation.findAll", query="SELECT o FROM Compensation o")
public class Compensation implements Serializable {
private static final long serialVersionUID = 1L;

/*@Id
@Column(name="ORDER_ID")
private String orderId;*/

@Column(name="CHANNEL_DEALER_CODE")
private String channelDealerCode;

//bi-directional one-to-one association to Order

@Id
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="ORDER_ID")
private Order order;


}

服务等级:

package com.sample.service;

import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.newModel.Order;


@Component
public class MobilityServiceImpl implements MobilityService {


@Autowired
private MobilityRepository mobilityRepo;

@Override
public Response getOrderDetails(String orderId) {

Order orderDetails=mobilityRepo.findByOrderId(orderId);

return Response.ok(orderDetails).build();
}

@Override
public Response saveOrderDetails(Order orderDetails) {

orderDetails.getCompensation().setOrder(orderDetails);

Order orderResponse =mobilityRepo.save(orderDetails);
String resp=orderResponse.getOrderId()+" is Success";
return Response.ok(resp).build();
}

}

控制台日志:

 Hibernate: select order0_.order_id as order_id1_1_1_, order0_.access_id as access_i2_1_1_, compensati1_.order_id as order_id2_0_0_, compensati1_.channel_dealer_code as channel_1_0_0_ from orders order0_ left outer join compensation compensati1_ on order0_.order_id=compensati1_.order_id where order0_.order_id=?
Hibernate: select compensati0_.order_id as order_id2_0_0_, compensati0_.channel_dealer_code as channel_1_0_0_ from compensation compensati0_ where compensati0_.order_id=?
Hibernate: insert into orders (access_id, order_id) values (?, ?)
Hibernate: insert into compensation (channel_dealer_code, order_id) values (?, ?)
2018-11-23 16:13:53.210 WARN 17532 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: -10, SQLState: 23502
2018-11-23 16:13:53.211 ERROR 17532 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : integrity constraint violation: NOT NULL check constraint; SYS_CT_10118 table: COMPENSATION column: ORDER_ID
2018-11-23 16:13:53.214 ERROR 17532 --- [nio-8080-exec-1] o.h.i.ExceptionMapperStandardImpl : HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
2018-11-23 16:13:53.244 ERROR 17532 --- [nio-8080-exec-1] 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

org.hsqldb.HsqlException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10118 table: COMPENSATION column: ORDER_ID

JSON 请求:

{
"orderId": "1006730",
"accessId": "1810_CRU",
"compensation": {

"channelDealerCode": "ABCD"
}
}

最佳答案

Compensation 实体中,您仍然需要在单独的属性中拥有 idOrder 并使用 MapsId 来拥有相同的 id

@Entity
@Table(name="COMPENSATION")
@NamedQuery(name="Compensation.findAll", query="SELECT o FROM Compensation o")
public class Compensation implements Serializable {
private static final long serialVersionUID = 1L;

@Id
private String id;

@Column(name="CHANNEL_DEALER_CODE")
private String channelDealerCode;

@OneToOne(fetch = FetchType.LAZY)
@MapsId
private Order order;
}

关于java - Hibernate完整性约束冲突: NOT NULL check constraint: For onetoOne Mapping using spring boot crud,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53448843/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com