Search code examples
javahibernatejpajakarta-eetransient

Exception when using @Transient in hibernate


i want to have a Extra variable in bean class so that i used @Transient on the extra member variable which is String date. as i red in some other tutorial

To have Extra column as follows

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;
    }
}

but i am geeing below Exception

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

is there any mistake in my bean class to have extra member variable ?

or

is there any other way to do so ? can any one help me?


Solution

  • From 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.

    So, you need to put @Transient on the getter.