Hibernate mappedBy 注解踩坑

今天,工作中遇到将mappedBy 注解加到 Getter公共属性上编译不通过,但是加到私有的属性字段上就可以编译通过的问题。

最后在Stack Overflow上找到答案。

原因是私有的private域 和公有的getter属性上注解混用造成的。(oderId域上有@Id注解,其他的注解全在公有的getter属性上)

I got the same problem with @ManyToOne
column. It was solved... in stupid way. I had all other annotations for public getter methods, because they were overridden from parent class. But last field was annotated for private variable like in all other classes in my project. So I got the same MappingException without the reason.
Solution: I placed all annotations at public getter methods. I suppose, Hibernate can't handle cases, when annotations for private fields and public getters are mixed in one class.

https://stackoverflow.com/questions/6164123/org-hibernate-mappingexception-could-not-determine-type-for-java-util-set

出错代码
package com.zx.stlife.entity.order;

import com.zx.stlife.constant.Const;
import com.zx.stlife.entity.SuperIntVersion;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "ngy_order")
public class NgyOrder{
    public NgyOrder() {

    }

    private Set<NgyOrderItem> orderItemsSet;

    private NgyOrderShipping ngyOrderShipping;

    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "uuid")
    private String orderId;

    private String title;

    private Double totalPayFee;

    @OneToMany(mappedBy="ngyOrder",fetch=FetchType.LAZY)
    public Set<NgyOrderItem> getOrderItemsSet() {
        return orderItemsSet;
    }

    public void setOrderItemsSet(Set<NgyOrderItem> orderItemsSet) {
        this.orderItemsSet = orderItemsSet;
    }


    @OneToOne(mappedBy="ngyOrder",fetch=FetchType.LAZY)
    public NgyOrderShipping getNgyOrderShipping() {
        return ngyOrderShipping;
    }

    public void setNgyOrderShipping(NgyOrderShipping ngyOrderShipping) {
        this.ngyOrderShipping = ngyOrderShipping;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Double getTotalPayFee() {
        return totalPayFee;
    }

    public void setTotalPayFee(Double totalPayFee) {
        this.totalPayFee = totalPayFee;
    }
正确代码
package com.zx.stlife.entity.order;

import com.zx.stlife.constant.Const;
import com.zx.stlife.entity.SuperIntVersion;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "ngy_order")
public class NgyOrder{
    public NgyOrder() {

    }

    private Set<NgyOrderItem> orderItemsSet;

    private NgyOrderShipping ngyOrderShipping;


    private String orderId;

    private String title;

    private Double totalPayFee;

    @OneToMany(mappedBy="ngyOrder",fetch=FetchType.LAZY)
    public Set<NgyOrderItem> getOrderItemsSet() {
        return orderItemsSet;
    }

    public void setOrderItemsSet(Set<NgyOrderItem> orderItemsSet) {
        this.orderItemsSet = orderItemsSet;
    }


    @OneToOne(mappedBy="ngyOrder",fetch=FetchType.LAZY)
    public NgyOrderShipping getNgyOrderShipping() {
        return ngyOrderShipping;
    }

    public void setNgyOrderShipping(NgyOrderShipping ngyOrderShipping) {
        this.ngyOrderShipping = ngyOrderShipping;
    }

    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "uuid")
    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Double getTotalPayFee() {
        return totalPayFee;
    }

    public void setTotalPayFee(Double totalPayFee) {
        this.totalPayFee = totalPayFee;
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 32,278评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,805评论 19 139
  • 2008-09-23 23:04 我不知道是自己太敏感还是生活真是有第六种味道的存在! 掐指一算,已经上学足足有1...
    平心如我阅读 2,206评论 0 0
  • 今天画了很久。实在是有点困。睡会睡会。还是比较满意的了……
    帅的起飞阅读 1,553评论 2 1
  • 01 偶然间,得知以前一起共事的同事涛离婚了,很是震惊。 时光倒退到我们刚参加工作时,那时的涛是个活泼阳光的大男孩...
    果茉莉阅读 4,419评论 2 9

友情链接更多精彩内容