221

I thought hibernate takes into consideration only instance variables that are annotated with @Column. But strangely today when I added a variable (that is not mapped to any column, just a variable i need in the class), it is trying to include that variable in the select statement as a column name and throws the error -

Unknown column 'team1_.agencyName' in 'field list'

My class -

@Entity
@Table(name="team")
public class Team extends BaseObject implements Serializable {

@Id  @GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

@Column(length=50)
private String name;

@Column(length=10)
private String code;

@Column(name = "agency_id")
private Long agencyId;

private String agencyName; //note: not annotated.

}

FYI...I use the above class in another class with many to many mapping

@ManyToMany(fetch = FetchType.EAGER) 
@JoinTable(
        name="user_team",
        joinColumns = { @JoinColumn( name="user_id") },
        inverseJoinColumns = @JoinColumn( name="team_id")
)    
public Set<Team> getTeams() {
    return teams;
}

Why is this happening?!

t0r0X
  • 4,212
  • 1
  • 38
  • 34
hese
  • 3,397
  • 8
  • 25
  • 34
  • 3
    very sorry...i am new to this. will do it for all my posts! thanks for telling. – hese Jan 12 '11 at 14:51
  • 1
    @Tunaki no, this is not exactly duplicate of question "JPA fastest way to ignore a field during persistence?" (http://stackoverflow.com/questions/1281952/jpa-fastest-way-to-ignore-a-field-during-persistence) This ticket is about Hibernate, and the other one is talking about JPA instead. – Yuci Mar 23 '17 at 15:19
  • @hese does your base class have some variables? If yes does hibernate ignore them or take into account? – Shekhar Sahu May 28 '19 at 16:38

3 Answers3

472

JPA will use all properties of the class, unless you specifically mark them with @Transient:

@Transient
private String agencyName;

The @Column annotation is purely optional, and is there to let you override the auto-generated column name. Furthermore, the length attribute of @Column is only used when auto-generating table definitions, it has no effect on the runtime.

skaffman
  • 398,947
  • 96
  • 818
  • 769
95

For folks who find this posting through the search engines, another possible cause of this problem is from importing the wrong package version of @Transient. Make sure that you import javax.persistence.transient and not some other package.

CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • 1
    thanks. I was wondering if it should be beans, or persistence. – Nasir Nov 10 '14 at 20:14
  • 3
    thanks, was using org.springframework.data.annotation.Transient and was confused why it does not work. – Rudy Aug 03 '17 at 03:37
  • thank you!!! funny, was thinking, do i really need another google search, the accepted answer was so probable..... – tom Nov 05 '18 at 22:28
32

Placing @Transient on getter with private field worked for me.

    private String name;

    @Transient
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
t0r0X
  • 4,212
  • 1
  • 38
  • 34
Saumyaraj
  • 1,220
  • 3
  • 15
  • 37
  • 1
    Putting @Transient at getter level worked for me too. – coretechie Mar 30 '17 at 09:16
  • 1
    Is it no different from declaring the variable `@Transient`? – ADTC Mar 23 '18 at 00:36
  • 4
    @ADTC depending on the libraries used, some balk at annotations on getter/setters, others do not (recently noticed this with Moxy and Jackson; Moxy's great because it fails silently without reason), some don't like them to be mixed (between properties and getter/setters). Gotta love it. – Shawn Apr 26 '18 at 20:50