Example:
@Entity
class Table_A{
@Id
@generatedValue
private long ID;
@Column
private String name;
@Transient
private String otherName;
// getters and setters
public long getID()
{ return ID;}
public void setID(long ID)
{ this.ID = ID;}
public String getName()
{ return Name;}
public void setName(String name)
{ this.name = name;}
public String getOtherName()
{ return otherName;}
public void setOtherName(String otherName)
{ this.otherName = otherName;}
}
Query Example:
String sql = "SELECT Table_A.*, otherName
FROM Table_A INNER JOIN Other_Table ON Table_A.id = Other_Table.id";
List<Table_A> = em.createNativeQuery(sql, Table_A.class).getResultList();
NOTE:
“otherName” column is a field of Other_Table.
Other_Table is not an Entity
PROBLEM:
the problem is, I can’t store the “otherName” column result to the @Transient field of the Table_A entity. Is there any way of doing this? It always returns null.
The problem is that you’ve used the form of createNativeQuery that expects to be able to place the result directly into your class
Table_A– but of course theotherNamewon’t map.If you use the other form of createNativeQuery, that uses a named
@SqlResultSetMapping, you might be able to get the value ofotherNameinto a@ColumnResultthat you can use. Have a look at the example here.I’m afraid you’ll still probably have to manually call
setOtherName()at some point though 🙁