XXX上的@OneToOne或@ManyToOne引用了未知实体:java.util.Set

时间:2019-03-22 06:04:07

标签: java spring hibernate spring-boot jpa

我正在创建一个供稿表,该供稿表具有带注释的一对多映射和具有用户表的多对一映射。但是,当我将供稿实体中的user_table字段注释为@ManyToOne时,出现类似@OneToOne或@ManyToOne的错误,但在xxx上引用了一个未知实体:java .util.Set,但是当我使用@ManyToMany对其进行注释时,它不会抛出任何错误和表正在创建。谁能解释我为什么

  package com.incture.metrodata.entity;

    import java.util.Date;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;

    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.JoinTable;
    import javax.persistence.ManyToMany;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;

    import lombok.Getter;
    import lombok.Setter;
    import lombok.ToString;

    @Entity
    @Getter
    @Setter
    @ToString
    @Table(name = "FEEDS_DETAILS")
    public class FeedsDo implements BaseDo {

        /**
         * 
         */
        private static final long serialVersionUID = -2035537433672391211L;

        @Id
        @Column(name = "FEED_ID")
        private String feedId;

        @Column(name = "BODY")
        private String body;

        @Temporal(TemporalType.TIMESTAMP)
        @Column(name = "CREATED_AT")
        private Date createdAt;

        @Column(name = "CREATED_BY")
        private String createdBy;

        @Column(name = "TITLE")
        private String title;

        @Temporal(TemporalType.TIMESTAMP)
        @Column(name = "UPDATED_AT")
        private Date updatedAt;

        @Column(name = "UPDATED_BY")
        private String updatedBy;

        @Column(name = "IS_DELETED")
        private int isDeleted = 0;

        @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
        @JoinTable(name = "FEEDS_USERS", joinColumns = { @JoinColumn(name = "FEED_ID")}, inverseJoinColumns = { @JoinColumn(name = "USER_ID") })
        private Set<UserDetailsDo> user = new HashSet<UserDetailsDo>(0);

        @OneToMany(targetEntity = CommentsDo.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
        private List<CommentsDo> comments;

        @Override
        public Object getPrimaryKey()
        {
            return feedId;
        }
    }





package com.incture.metrodata.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "COMMENTS")
public class CommentsDo implements BaseDo {

    /**
     * 
     */
    private static final long serialVersionUID = 5180603287069572120L;

    @Id
    @Column(name = "COMMENT_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long commentId;

    @Lob
    private String comment;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATED_AT")
    private Date createdAt;

    @Column(name = "CREATED_BY")
    private String createdBy;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "UPDATED_AT")
    private Date updatedAt;

    @Column(name = "IS_DELETED")
    private int isDeleted=0;

    @Override
    public Object getPrimaryKey() {

        return commentId;
    }
}


    package com.incture.metrodata.entity;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Where;

import lombok.Data;
import lombok.ToString;

@Entity
@Data
@ToString
@Table(name = "USER_DETAILS")
@DynamicUpdate(true)
@Where(clause = "DELETE = 0")
public class UserDetailsDo implements BaseDo {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "USER_ID",length=50)
    private String userId;

    @Column(name = "FIRST_NAME",length=100)
    private String firstName;

    @Column(name = "LAST_NAME",length=100)
    private String lastName;

    //@Formula(value = " concat(FIRST_NAME, ' ', LAST_NAME) ")
    @Column(name = "NAME",length=100)
    private String name;

    @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    private RoleDetailsDo role;

    @Column(name = "TELEPHONE",length=50)
    private String telephone;

    @Column(name = "CREATED_BY",length=50)
    private String createdBy;

    @Column(name = "UPDATED_BY",length=50)
    private String updatedBy;


    @Column(name = "MOBILE_TOKEN")
    @Lob
    private String mobileToken;

    @Column(name = "WEB_TOKEN")
    @Lob
    private String webToken;

    @Column(name = "LONGITUDE")
    private Double longitude;

    @Column(name = "LATITUDE")
    private Double latitude;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATED_DATE")
    private Date createdDate;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATED_AT")
    private Date createdAt;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "UPDATED_AT")
    private Date updateAt;

    @Column(name = "EMAIL",length=100)
    private String email;

    @Column(name = "PARENT_ID",length=100)
    private String parentId;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "LAST_LOGIN_TIME")
    private Date lastLogedIn;

    @Column(name = "TRACK_FREQUENCY")
    @ColumnDefault("'30'")
    private Long trackFreq;

    @ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    @JoinTable(name = "USERS_WAREHOUSE_MAPPING", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = {
            @JoinColumn(name = "WARE_HOUSE_ID") })
    private Set<WareHouseDetailsDo> wareHouseDetails = new HashSet<WareHouseDetailsDo>(0);

    @ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    @JoinTable(name = "USERS_COURIER_MAPPING", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = {
            @JoinColumn(name = "COURIER_ID") })
    private Set<CourierDetailsDo> courierDetails = new HashSet<CourierDetailsDo>(0);

    @Column(name = "DELETE")
    @ColumnDefault("'0'")
    private Integer deleted = 0;

    public void setDeleted() {
        this.deleted = 1;
    }

    @Override
    public Object getPrimaryKey() {
        return userId;
    }

}

1 个答案:

答案 0 :(得分:3)

您具有单向关系:

@OneToMany(targetEntity = CommentsDo.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<CommentsDo> comments;

并且休眠不知道要使用哪个列来连接这些实体。最好是在子方面添加关系定义。您需要指定@JoinColumn,例如:

@JoinColumn(name = "feeds_do_id")
private FeedsDo feedsDo;
CommentsDo类中为

。应该有一个外键而不是feeds_do_id