11

I am using Hibernate to map with MySQL

I have an entity class in which I have the methods mapped with columns in MySQL

The question is, if its possible that I do not map some of the method in that class with any column in SQL, as if i try not to map one of my method in entity class, it gives exception.

Here is the code snippet

@Column(name="skills")
public String getSkills() {
    return skills;
}

public int getRowCount() {
    return rowCount;
}

In this above code I have assigned getSkills with Column skills in SQL, but I do not want to assign getRowCount() with any column in MySQL.

How could i achieve that (as in this above situation its giving exception, rowCount is unknown)?

skaffman
  • 398,947
  • 96
  • 818
  • 769
junaidp
  • 10,801
  • 29
  • 89
  • 137

1 Answers1

28

Use the @Transient annotation:

This annotation specifies that the property or field is not persistent. It is used to annotate a property or field of an entity class, mapped superclass, or embeddable class.

i.e.

@Column(name="skills")
public String getSkills() {
    return skills;
}

@Transient
public int getRowCount() {
    return rowCount;
}
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • Thanks for your help ,Error is gone , but i am trying to SET some value in the rowCount by its setter and then i try to get that value from this getter public int getRowCount() { return rowCount; } like i set it 4 and it saves there,but when i try to getRowCount it always have 0, any solution ? thanks – junaidp May 24 '11 at 09:44
  • @user764446: You either let Hibernate manage it, or you manage it. If it's marked as `@Transient`, then it's up to you to manage the value. – skaffman May 24 '11 at 14:42
  • yes,i am trying to manage it myself ,but i am not getting the value which i set there,every other method giving me the true value ,except this method,as other method have been mapped with the columns in database – junaidp May 25 '11 at 02:48
  • Doesn't work on List fields. For Collection type fields, hibernate actually creates a new intermediate table. Any workaround for that ? – zulkarnain shah Aug 29 '17 at 09:12