[英]Not null constraint violation when flushing unidirectional entities
I am playing a little bit with unidirectional and bidirectional mappings using @OneToMany
and @ManyToOne
annotations, but I cannot break the wall through for unidirectional one when persisting entities and flushing them into the database.我正在使用
@OneToMany
和@ManyToOne
注释进行单向和双向映射,但是在持久化实体并将它们刷新到数据库中时,我无法打破单向映射的障碍。
So, two tables delivery_company
might have many delivery
:因此,两个表
delivery_company
可能有很多delivery
:
SQL (Oracle): SQL(甲骨文):
CREATE TABLE delivery (
delivery_id NUMBER(6) NOT NULL,
price NUMBER(5, 2) NOT NULL,
delivery_time DATE NOT NULL,
delivery_company_id NUMBER(2) NOT NULL
);
ALTER TABLE delivery ADD CONSTRAINT delivery_pk PRIMARY KEY ( delivery_id );
CREATE TABLE delivery_company (
delivery_company_id NUMBER(2) NOT NULL,
delivery_company_name VARCHAR2(15 CHAR) NOT NULL
);
ALTER TABLE delivery_company ADD CONSTRAINT delivery_company_pk PRIMARY KEY ( delivery_company_id );
ALTER TABLE delivery
ADD CONSTRAINT delivery_delivery_company_fk FOREIGN KEY ( delivery_company_id )
REFERENCES delivery_company ( delivery_company_id );
Unidirectional mapping:单向映射:
@Entity
@Table(name = "Delivery")
class DeliveryUniDirectional
{
@Id
@SequenceGenerator(name = "delivery_id_sequence", sequenceName = "delivery_id_sequence", allocationSize = 1)
@GeneratedValue(generator = "delivery_id_sequence", strategy = GenerationType.SEQUENCE)
@Column(name = "delivery_id")
public Long deliveryId;
public BigDecimal price;
@Temporal(TemporalType.DATE)
public Date deliveryTime;
// setters, getters
}
@Entity
@Table(name = "delivery_company")
class DeliveryCompanyUniDirectional {
@Id
@Column(name = "delivery_company_id")
@SequenceGenerator(name = "delivery_company_id_sequence", sequenceName = "delivery_company_id_sequence", allocationSize = 1)
@GeneratedValue(generator = "delivery_company_id_sequence", strategy = GenerationType.SEQUENCE)
private Long deliveryCompanyId;
@Column(unique = true)
private String deliveryCompanyName;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "delivery_id", nullable = false, insertable = false, updatable = false)
private List<DeliveryUniDirectional> deliveries = new LinkedList<>();
// setters getters
}
When I run @DataJpaTest
test:当我运行
@DataJpaTest
测试时:
@Test
public void insertDeliveryUniDirectional()
{
DeliveryCompanyUniDirectional deliveryCompany = new DeliveryCompanyUniDirectional();
deliveryCompany.setDeliveryCompanyName("aa");
DeliveryUniDirectional delivery = new DeliveryUniDirectional();
delivery.setPrice(BigDecimal.ONE);
delivery.setDeliveryTime(new Date());
deliveryCompany.getDeliveries().add(delivery);
entityManager.persist(deliveryCompany);
entityManager.flush();
}
I receive我收到
javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute batch ...
//
Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("TESTUSER"."DELIVERY"."DELIVERY_COMPANY_ID")
when entityManager.flush();
当
entityManager.flush();
. .
I tried in DeliveryCompanyUniDirectional
to use @JoinColumn
without insertable and updatable, but in that case hibernate complains:我尝试在
DeliveryCompanyUniDirectional
中使用@JoinColumn
而不插入和更新,但在这种情况下 hibernate 抱怨:
Error creating bean with name 'entityManagerFactory' defined in class path resource ...
// ...
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: wieczorek.jakub.shop.business.spring.model.domain.DeliveryUniDirectional column: delivery_id (should be mapped with insert="false" update="false")
Definitely there is a problem with NOT NULL
constraint for the foreign key in delivery
table. delivery
表中外键的NOT NULL
约束肯定存在问题。 When I try it with bidirectional mapping, persisting and flushing work very good, but I would like to achieve the same using unidirectional.当我尝试使用双向映射时,持久化和刷新效果非常好,但我想使用单向来实现相同的效果。
Thanks for reading谢谢阅读
Your @JoinColumn should be delivery_company_id
since it's your foreign key您的 @JoinColumn 应该是
delivery_company_id
因为它是您的外键
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="delivery_company_id", referencedColumnName="delivery_company_id", nullable = false)
private List<DeliveryUniDirectional> deliveries = new LinkedList<>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.