美文网首页
Hibernate mappedBy 注解踩坑

Hibernate mappedBy 注解踩坑

作者: congnie116 | 来源:发表于2017-08-23 16:13 被阅读0次

今天,工作中遇到将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;
    }

相关文章

  • Hibernate mappedBy 注解踩坑

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

  • 【JPA】JPA@Many注解

    (转)ManyToMany注解级联情况 @ManyToMany级联情况 mappedby作用 使用mappedby...

  • Hibernate学习笔记 - 第003天

    mappedBy mappedBy 一定是多得一方维护关联关系使用这个注解 MIME类型 Multipurpose...

  • Hibernate注解(一)类级别注解

    1、Hibernate注解简介 2、JPA与Hibernate的关系 3、Hibernate注解分类 4、@Ent...

  • JPA

    配置 maven 配置Hibernate 配置自动扫描 编码 实体类 使用mappedBy的属性所表示的对象,是需...

  • 效率提升办法

    1、实体构造,使用@Builder注解,但要注意踩坑@Builder@AllArgsConstructor@NoA...

  • Hibernate注解

    注解 同样的,Hibernate也是支持使用注解方式的。创建hibernate应用程序时有许多注解可用,如@Ent...

  • 框架注解解析

    一,通用注解 二,Spring 三,mybatis/mybatis-plus注解 四,hibernate注解

  • JPA 注解学习

    最近学习hibernate注解形式配置POJO类,将注解的解析记下来,以备以后使用。 例1. Hibernate ...

  • @Secure注解笔记

    使用示例 踩坑记录:1、前置条件:必须在@Configuration类中加入如下注解 2、注解内容:大括号中的字符...

网友评论

      本文标题:Hibernate mappedBy 注解踩坑

      本文链接:https://www.haomeiwen.com/subject/ymaddxtx.html