简体   繁体   English

刷新单向实体时不是 null 约束违反

[英]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.

相关问题 删除实体时违反约束 - Constraint violation when deleting entities Hibernate:保存前刷新时违反约束 - Hibernate : Constraint violation when flushing just before saving 持久保留关联实体时休眠约束违规 - Hibernate Constraint Violation when persisting associated entities 在删除元素时,Hibernate单向OneToMany映射中的约束违反,包括JoinTable和OrderColumn - Constraint violation in Hibernate unidirectional OneToMany mapping with JoinTable and OrderColumn when removing elements 所有者删除时违反参照完整性约束(OneToMany单向) - Referential integrity constraint violation on owner delete (OneToMany unidirectional) 违反完整性约束:NOT NULL检查约束 - integrity constraint violation: NOT NULL check constraint SQl 错误 - 一次持久化多个实体时违反参照完整性约束 - SQl error - Referential integrity constraint violation when persisting several entities at once Hibernate 单向一对多关系与外键中的非空约束 - Hibernate Unidirectional OneToMany Relationship with Not-null Constraint in Foreign Key 使用@GeneratedValue对@Id进行Hibernate空约束违规 - Hibernate null constraint violation on @Id with @GeneratedValue 多对一单向禁用约束 - ManyToOne Unidirectional Disable Constraint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM