@AttributeOverride和manyToMany在Hibernate中不起作用

时间:2018-02-18 07:14:24

标签: java hibernate jpa

这是Generic class:

@MappedSuperclass
public abstract class BaseEntity<T> implements Serializable {

    private static final long serialVersionUID = 4295229462159851306L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence_db")
    private T id;

    @JsonIgnore
    @XmlTransient

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "createdby", updatable = false)
    private User createdBy;

    @JsonIgnore
    @XmlTransient

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "updatedby")
    private User updatedBy;

    @Column(name = "createddate", updatable = false)
    private Date createdDate = new Date();

    @Column(name = "updateddate")
    private Date updatedDate = new Date();

    @Column(name = "ip")
    private String ip = "127.0.0.1";

    @Transient
    private Integer version = 0;

    //@Formula("count(*) over ()")
    @Transient
    private Integer totalRecords;

    public BaseEntity() {
        super();
    }

    public BaseEntity(T id) {
        super();
        this.id = id;
    }

    public T getId() {
        return id;
    }

    public void setId(T id) {
        this.id = id;
    }

    @JsonIgnore
    public User getCreatedBy() {
        return createdBy;
    }

    @JsonIgnore
    public void setCreatedBy(User createdBy) {
        this.createdBy = createdBy;
    }

    @JsonIgnore
    public User getUpdatedBy() {
        return updatedBy;
    }

    @JsonIgnore
    public void setUpdatedBy(User updatedBy) {
        this.updatedBy = updatedBy;
    }

    @JsonIgnore
    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    @JsonIgnore
    public Date getUpdatedDate() {
        return updatedDate;
    }

    public void setUpdatedDate(Date updatedDate) {
        this.updatedDate = updatedDate;
    }

    @JsonIgnore
    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    @JsonIgnore
    public Integer getTotalRecords() {
        return totalRecords;
    }

    @JsonIgnore
    public void setTotalRecords(Integer totalRecords) {
        this.totalRecords = totalRecords;
    }

}

以下类是从Generic类扩展而来的。

@Entity
@Table(name = "SMP_SAMAPEL_CODE")
@SequenceGenerator(name = "sequence_db", sequenceName =    "SEQ_CORE_ITEM", allocationSize = 1)
@AttributeOverride(name = "ID", column = @Column(name = "MAINCODE"))
public class Item extends BaseEntity<Long> {

    @Column(name = "GROUP")
    private String group;

另一个类与ManyToMany的关系如下:

    ....
    @ManyToMany(fetch = FetchType.LAZY)
        @JoinTable(name = "CORE_CATEGORY_item",
                joinColumns =
                @JoinColumn(name = "CATEGORY_ID", referencedColumnName = "ID", nullable = false),
                inverseJoinColumns =
                @JoinColumn(name = "ITEM_ID", referencedColumnName = "MAINCODE", nullable = false)
        )
        private Set<Item> items;

....

在Item类之上没有@AttributeOverride(name = "ID", column = @Column(name = "MAINCODE"))的情况下正常工作,但是当它放在Item类的顶部时,会引发以下异常:

  

引起:org.hibernate.MappingException:无法找到列   逻辑名称:MAINCODE in   org.hibernate.mapping.Table(amd.SMP_SAMAPEL_CODE)及其相关   在supertables和次要表   org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:826)

它是如何修复的?

EDIT1:

数据库中Item实体的ID为MAINCODE

1 个答案:

答案 0 :(得分:0)

使用Hibernate时无法覆盖实体标识符。

JPA规范并没有说这也应该得到支持。