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

今天用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方法  ,最后才发现不是这个原因(原谅我是菜鸟),关键是出错在这里,上图:

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

 

 

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

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无人交流的问题

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值