春季启动错误org.hibernate.MappingException:无法确定类型

时间:2019-02-08 17:32:34

标签: java spring hibernate spring-boot jpa

我正在使用Spring启动项目,并收到以下错误消息

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: Could not determine type for: com.appoint.manager.appointment.models.Status, at table: appointment, for columns: [org.hibernate.mapping.Column(status)]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1745) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:853) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at com.appoint.manager.appointment.AppointmentApplication.main(AppointmentApplication.java:16) [classes/:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: com.appoint.manager.appointment.models.Status, at table: appointment, for columns: [org.hibernate.mapping.Column(status)]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:402) ~[spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) ~[spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    ... 16 common frames omitted



Caused by: org.hibernate.MappingException: Could not determine type for: com.appoint.manager.appointment.models.Status, at table: appointment, for columns: [org.hibernate.mapping.Column(status)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:486) ~[hibernate-core-5.4.1.Final.jar:5.4.1.Final]
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:453) ~[hibernate-core-5.4.1.Final.jar:5.4.1.Final]
    at org.hibernate.mapping.Property.isValid(Property.java:226) ~[hibernate-core-5.4.1.Final.jar:5.4.1.Final]
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:624) ~[hibernate-core-5.4.1.Final.jar:5.4.1.Final]
    at org.hibernate.mapping.RootClass.validate(RootClass.java:267) ~[hibernate-core-5.4.1.Final.jar:5.4.1.Final]
    at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:343) ~[hibernate-core-5.4.1.Final.jar:5.4.1.Final]
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:461) ~[hibernate-core-5.4.1.Final.jar:5.4.1.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:938) ~[hibernate-core-5.4.1.Final.jar:5.4.1.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) ~[spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) ~[spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    ... 20 common frames omitted

下面提供了模型类,

@Entity
public class Appointment {


//    id
//    created_at
//    appointment_date
//    name_of_doctor
//    status (Available or Booked)
//    price

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private java.sql.Time craeted_at;

    @Column
    private java.sql.Date appointment_date;

    @Column
    private String name_of_doctor;

//    @Column
////    @JsonProperty("status")
//    private Boolean status;

    @Column
    private Status status;

    @Column
    private double price;

    public Appointment() {

    }


    @JsonCreator
    public Appointment(@JsonProperty("craeted_at") Time craeted_at, @JsonProperty("appointment_date") Date appointment_date,
                       @JsonProperty("name_of_doctor") String name_of_doctor, @JsonProperty("status") Status status, @JsonProperty("price") double price) {

        this.craeted_at = craeted_at;
        this.appointment_date = appointment_date;
        this.name_of_doctor = name_of_doctor;
        this.status = status;
        this.price = price;
    }

    public Appointment(String name_of_doctor, Status status, double price) {

        this.name_of_doctor = name_of_doctor;
        this.status = status;
        this.price = price;
    }


    public Appointment(String name_of_doctor, double price) {

        this.name_of_doctor = name_of_doctor;
        this.price = price;
    }

    public Long getId() {
        return id;
    }

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

    public Time getCraeted_at() {
        return craeted_at;
    }

    public void setCraeted_at(Time craeted_at) {
        this.craeted_at = craeted_at;
    }

    public Date getAppointment_date() {
        return appointment_date;
    }

    public void setAppointment_date(Date appointment_date) {
        this.appointment_date = appointment_date;
    }

    public String getName_of_doctor() {
        return name_of_doctor;
    }

    public void setName_of_doctor(String name_of_doctor) {
        this.name_of_doctor = name_of_doctor;
    }

    public Status isStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Appointment)) return false;
        Appointment that = (Appointment) o;
        return Double.compare(that.getPrice(), getPrice()) == 0 &&
                Objects.equals(getId(), that.getId()) &&
                Objects.equals(getCraeted_at(), that.getCraeted_at()) &&
                Objects.equals(getAppointment_date(), that.getAppointment_date()) &&
                Objects.equals(getName_of_doctor(), that.getName_of_doctor()) &&
                Objects.equals(status, that.status);
    }

    @Override
    public int hashCode() {

        return Objects.hash(getId(), getCraeted_at(), getAppointment_date(), getName_of_doctor(), status, getPrice());
    }


    @Override
    public String toString() {
        return "Appointment{" +
                "id=" + id +
                ", craeted_at=" + craeted_at +
                ", appointment_date=" + appointment_date +
                ", name_of_doctor='" + name_of_doctor + '\'' +
                ", status=" + status +
                ", price=" + price +
                '}';
    }


}






public class Status {

    Boolean status;

    public Status(Boolean status) {
        this.status = status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }
}

对我来说很明显,这是一个长期存在的问题,ORM无法确定Status类的类型。如何纠正这个问题,我可以通过注释显式定义Status对象类型吗?

1 个答案:

答案 0 :(得分:1)

您没有Status的映射。例如,可以使用@Embeddable@Embedded来完成此操作(我假设列名是“ status”):

@Embeddable
public class Status {

    @Column(name = "status")
    Boolean status;

    public Status(Boolean status) {
        this.status = status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }
}

然后将其放置在@Column实体中,而不是Appointment

@Embedded
private Status status;

Here详细了解了@Embeddable的可重用性。

相关问题