开发者社区> 问答> 正文

使用Hibernate注解实现实体类映射,遇到的持久化问题? 400 报错

使用Hibernate注解实现实体类映射,遇到的持久化问题? 400 报错 A 类和B类是一对一关系,A的主键自增长,且作为B的外键和主键,执行insertorupdate(A)的时候出错,不能执行插入操作是怎么回事。

展开
收起
爱吃鱼的程序员 2020-06-03 17:09:04 622 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    我说得有点混乱######也许你应该简化你的逻辑,先save主表再save子表,反正你的是一对一关系 那么只要主表save成功了紧接着去save子表已经达成了一对一的关系 外键也许不起多大作用,反之主表save失败了事务会回滚,也没影响######

    @MapId



    http://java.dzone.com/articles/onetoone-shared-primary-key

    ######错误信息呢######

    引用来自“CrazyHarry”的评论

    错误信息呢
    2014-11-20 12:50:26.214:WARN:oejs.ServletHandler:
    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.inhdu.career.modules.cms.entity.ArticleData

    ######nested exception is org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.inhdu.career.modules.cms.entity.ArticleData 
    这里已经说了 :在save之前要给对象指定ID,那么你既然要让主键自增,你使用注解的时候ID有类似下述注解吗:
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    ######

    把两个类的主键和关联配置贴出来

    ######

    引用来自“CrazyHarry”的评论

    nested exception is org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.inhdu.career.modules.cms.entity.ArticleData 
    这里已经说了 :在save之前要给对象指定ID,那么你既然要让主键自增,你使用注解的时候ID有类似下述注解吗:
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    article 类内有个articledata 类型地属性,他们两个是一对一关系,article类有 @Id 
    @GeneratedValue,在articledata的id属性加上了@Id,此类里边的article属性的get方法上加了@OneToOne和
    @PrimaryKeyJoinColumn

    但是save的是article这个类,然后类里边articledata的id是空的,结果就导致了上边的错误。

    我是不知道有什么方法在article没有完成持久化的时候给aticledata的id set进去一个值,因为这个articledata的id在数据表里边是主键

    ######简单一点,分两次保存,手动设置 articledata 的ID吧
    ######先保存主的,再搞副的
    2020-06-03 20:35:49
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载