今天用spring boot +springdata jpa 构建项目,想在javaBean中直接创建数据库的表,因为一个表中的主键有两个,然后一开始按照网上的写法,一直出现这个错误。然后调试了一会原来是自己太马虎,先把报的错贴出来:

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 org.hibernate.AssertionFailure: Subclass has to be binded after it's mother class: com.transportation.model.TruckAndPhoto
 

Caused by: org.hibernate.AssertionFailure: Subclass has to be binded after it's mother class: com.transportation.model.TruckAndPhoto
 

从字面意思也就是  : 子类必须在母类之后绑定

然后先把错误的代码先拿出来:

 

 

Photo 类


@SuppressWarnings("serial")
@Entity
@Table(name = "Photo")
public class Photo extends  TruckAndPhoto {
       
    
    @EmbeddedId
    private TruckAndPhoto photo_id;
    @Column(name="photo_address")
    private String photo_address;
 

    @Override
    public String toString() {
        return "Photo [photo_id=" + photo_id + ", photo_address=" + photo_address + "]";
    }
    
}
 

TruckAndPhoto 类


@Embeddable
@SuppressWarnings("serial")
public class TruckAndPhoto implements Serializable{

    
    private int photo_id;
    private int truck_id;
    public TruckAndPhoto() {
        super();
    }
    public TruckAndPhoto(int photo_id, int truck_id) {
        super();
        this.photo_id = photo_id;
        this.truck_id = truck_id;
    }
    public int getPhoto_id() {
        return photo_id;
    }
    public void setPhoto_id(int photo_id) {
        this.photo_id = photo_id;
    }
    public int getTruck_id() {
        return truck_id;
    }
    public void setTruck_id(int truck_id) {
        this.truck_id = truck_id;
    }
}
也就是 TruckAndPhoto类中的 photo_id和truck_id 为Photo 表的主键,也就是联合主键,然后报了错,一开始一直以为是没有重写equalshashCode方法  ,最后才发现不是这个原因(原谅我是菜鸟),关键是出错在这里,上图:

Caused by: org.hibernate.AssertionFailure: Subclass has to be binded after it's mother class:

这里的extends TruckAndPhoto 出了错 ,然后将extends TruckAndPhoto 该为implements Serializable  就对啦!

Caused by: org.hibernate.AssertionFailure: Subclass has to be binded after it's mother class:

 

Caused by: org.hibernate.AssertionFailure: Subclass has to be binded after it's mother class:

 

本文原创,转发请附上博主小名,谢谢~

 

 

相关文章:

  • 2021-06-17
  • 2021-12-23
  • 2022-01-02
  • 2021-05-31
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-15
  • 2021-05-19
  • 2021-04-25
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案