Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a variable in Entity class which is not a column

In my WAS application, I have a requirement to define a variable(String) in an Entity class that maps to a table. So the fields that are related to the table have annotation as @Column(name="column_name") I have a requirement to add a new field/variable to this Entity class that is not a column in table. If I declare it as a normal field, JPA converts this field also in the SQLs. This is causing SQL -206 error(as expected).

How to do I declare this field? Is there an annotation that I can use to say its a custom variable and not related to any of the columns in the table defined by this Entity?

example:

@Entity
@Table(name = "TABLE_1")
@NamedQueries(
 {
@NamedQuery(name = "abc:findTableContent", query = "SELECT u FROM TABLE_1 u WHERE u.Id = :iD"),
 })
public class TableEntity1 implements Serializable
{
    @Id
    @Column(name = "TABLE1_ID")
    private Long iD;

    @Column(name = "DESC")
    private String desc;


    private String error;    


}

So if I run the namedquery, it gets executed as "SELECT t0.iD, t0.desc, t0.error FROM TABLE_1 u WHERE u.iD=?"

How do I solve this? Thanks for your help!

like image 390
user726579 Avatar asked Mar 06 '14 23:03

user726579


1 Answers

I found the answer. I could mark the field or variable as @javax.persistence.Transient

like image 103
user726579 Avatar answered Sep 22 '22 20:09

user726579