gpt4 book ai didi

java - Spring启动错误org.hibernate.MappingException : Could not determine type

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

我使用 Spring boot 项目并收到以下错误,

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 对象类型吗?

最佳答案

您没有状态的映射。例如,可以使用 @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;
}
}

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

@Embedded
private Status status;

Here更多了解 @Embeddable 的可重用性。

关于java - Spring启动错误org.hibernate.MappingException : Could not determine type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54597549/

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