Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to ignore some field when mapping in annotation,Entity class [duplicate]

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)?

like image 630
junaidp Avatar asked May 24 '11 08:05

junaidp


People also ask

What annotation can exclude fields and properties of entity from mapping in JPA?

Introduction. When persisting Java objects into database records using an Object-Relational Mapping (ORM) framework, we often want to ignore certain fields. If the framework is compliant with the Java Persistence API (JPA), we can add the @Transient annotation to these fields.

How do you ignore a field in entity class to create a table?

Every non static non transient property (field or method depending on the access type) of an entity is considered persistent , unless you annotate it as @Transient . So you can use @Transient annotation on the property you do not wish to create a column for in database.

How do I ignore a field in JPA?

To ignore a field, annotate it with @Transient so it will not be mapped by hibernate.

What will happen if @table is not specified while defining pojo?

If no @Table is defined the default values are used: the unqualified class name of the entity. For example if you have: @Entity public class MyTest{ ... Your table will have the name my_test in your database.


1 Answers

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;
}
like image 164
skaffman Avatar answered Sep 27 '22 19:09

skaffman