具有未知实体和@Generated注释的MappingException

时间:2019-07-09 12:50:29

标签: java spring oracle hibernate

我想为我的前额键status_id设置默认值。 因此,我在Oracle内部创建了一个触发器,该触发器在插入时设置默认值。之后,Hibernate应该更新链接的外部实例。

我在Oracle中具有以下数据库:

CREATE TABLE status (
    status_id NUMBER(10) NOT NULL,
    name VARCHAR2(50) NOT NULL,
    CONSTRAINT status_pk PRIMARY KEY (status_id)
);

CREATE TABLE customer (
    customer_id NUMBER(10) NOT NULL,
    street VARCHAR2(50),
    zip VARCHAR2(50),
    city VARCHAR2(50),
    salutation VARCHAR2(50),
    title VARCHAR2(50),
    firstname VARCHAR2(50),
    surname VARCHAR2(50),
    phone VARCHAR2(50),
    fax VARCHAR2(50),
    email VARCHAR2(50),
    type VARCHAR2(50),
    status_id NUMBER(10) NOT NULL,
    CONSTRAINT customer_pk PRIMARY KEY (customer_id),
    CONSTRAINT customer_status_fk FOREIGN KEY (status_id) REFERENCES status(status_id)
);

INSERT INTO status (status_id, name) VALUES ('1', 'DEFAULT');
INSERT INTO status (status_id, name) VALUES ('2', 'PREMIUM');
INSERT INTO status (status_id, name) VALUES ('3', 'SUPPLIER');

如果要在插入新客户时未指定状态,我想将状态设置为“默认”。所以我创建了以下触发器:

CREATE OR REPLACE TRIGGER status_default_trigger BEFORE INSERT ON customer FOR EACH ROW BEGIN
    IF :NEW.type IS NULL THEN
        SELECT 'NEW' INTO :NEW.type FROM DUAL;
    END IF;
    IF :NEW.status_id IS NULL THEN
        SELECT status_id INTO :NEW.status_id FROM status WHERE status.status_id = 1;
    END IF;
 END;
/

“我的实体”如下所示: Status.java:

@Data
@Entity
@Table(name = "status")
public class Status {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private BigDecimal statusId;

    @OneToMany(mappedBy = "status", fetch = FetchType.LAZY)
    private List<Customer> customerList;

    @Column
    private String name;
}

Customer.java:

@Data
@Entity(name = "einsender")
@Table(name = "einsender")
public class Einsender {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private BigDecimal einsenderId;

    @Generated(GenerationTime.INSERT) // This fails to start Hibernate entityManager
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "status_id")
    private Status status;

    @Column
    private String street;

    @Column
    private String zip;

    @Column
    private String city;

    @Column
    private String salutation;

    @Column
    private String title;

    @Column
    private String firstname;

    @Column
    private String surname;

    @Column
    private String phone;

    @Column
    private String fax;

    @Column
    private String email;

    @Generated(GenerationTime.INSERT) // This works instead
    @Column
    private String type;
}

我希望Customer.Status将是默认的Status(statusId = 1,name ='DEFAULT);但是我得到以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Unknown entity: de.example.model.Status
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1762) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
...
Caused by: org.hibernate.MappingException: Unknown entity: de.bund.bamf.vup.service.vorgang.model.Status

事实上,如果我在@Generated(GenerationTime.INSERT)上方评论private Status status;,则将映射Customer.type值

0 个答案:

没有答案