在休眠中使用@Transient 时出现异常

Exception when using @Transient in hibernate

我想在 bean class 中有一个 额外变量,所以我在额外成员变量上使用了 @Transient这是字符串日期。正如我在其他一些教程中的红色

要有如下的Extra栏

package com.rasvek.cg.entity;
// Generated May 14, 2018 11:39:07 PM by Hibernate Tools 5.1.7.Final

import static javax.persistence.GenerationType.IDENTITY;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * FeeTermDates generated by hbm2java
 */
@Entity
@Table(name = "fee_term_dates", catalog = "campus_guru_01")
public class FeeTermDates implements java.io.Serializable {

    private int tdmId;
    private FeeTerms feeTerms;
    @Transient
    private String date;

    @Temporal(TemporalType.DATE)
    private Date termDate;
    public FeeTermDates() {
    }

    public FeeTermDates(int tdmId, FeeTerms feeTerms) {
        this.tdmId = tdmId;
        this.feeTerms = feeTerms;
    }

    public FeeTermDates(int tdmId, FeeTerms feeTerms, String date) {
        this.tdmId = tdmId;
        this.feeTerms = feeTerms;
        this.date = date;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "tdm_id", unique = true, nullable = false)
    public int getTdmId() {
        return this.tdmId;
    }

    public void setTdmId(int tdmId) {
        this.tdmId = tdmId;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "term_id", nullable = false)
    public FeeTerms getFeeTerms() {
        return this.feeTerms;  
    }  

    public void setFeeTerms(FeeTerms feeTerms) {
        this.feeTerms = feeTerms;
    }

    @Column(name = "date")
    public Date getTermDate() {
        return termDate;
    }

    public void setTermDate(Date termDate) {
        this.termDate = termDate;
    }
    public String getDate() {
        return this.date;
    }

    public void setDate(String date) {
        this.date = date;
    }
}

但我在异常以下

org.hibernate.MappingException: Repeated column in mapping for entity: com.rasvek.cg.entity.FeeTermDates column: date (should be mapped with insert="false" update="false")
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:835)
    at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:853)
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:875)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:607)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:265)
    at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:459)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:710)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
    at org.springframework.orm.hibernate5.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:511)
    at org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:495)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1688)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1626)
    ... 77 more

我的bean有没有错误class 有额外的成员变量

还有其他方法吗? 谁能帮帮我?

来自JSR 338: JavaTM Persistence API, Version 2.1

2.3.1 Default Access Type

When annotations are used to define a default access type, the placement of the mapping annotations on either the persistent fields or persistent properties of the entity class specifies the access type as being either field- or property-based access respectively.

  • When property-based access is used, the object/relational mapping annotations for the entity class annotate the getter property accessors[7], and the persistence provider runtime accesses persistent state via the property accessor methods. All properties not annotated with the Transient annotation are persistent.

因此,您需要将 @Transient 放在 getter 上。