Hello,
Our customer are using a legacy database full of constraints and triggers, and we are not allowed to change or add tables. Anyway, they decided to use hibernate3. This still means lots of native sql...
This is a simplification of my problem, but anyway:
The database table TESTTABLE has three columns FOOBAR, BARFOO and HIDDENFOO (number (pk), varchar, varchar)
And I have a POJO TestTable.java:
Code:
@Entity
@Table (name="TESTTABLE")
public class Test implements Serializable{
private Integer fooBar;
private String barFoo;
private String fax;
@Id
@Column(name="FOOBAR")
public Integer getFooBar() {
return fooBar;
}
public void setFooBar(Integer fooBar) {
this.fooBar = fooBar;
}
@Column(name="BARFOO")
public String getBarFoo() {
return barFoo;
}
public void setBarFoo(String barFoo) {
this.barFoo = barFoo;
}
//1. @Column(name="fax", insertable = false, updatable = false)
//2. @Transient
public String getFax() {
return fax;
}
public void setFax(String fax) {
this.fax = fax;
}
}
a) And somewhere I have this kind of code:
Code:
String qstr = "select TESTTABLE.FOOBAR as {tt.fooBar}, TESTTABLE.BARFOO as {tt.barFoo}, (TESTTABLE.BARFOO || '-' || TESTTABLE.HIDDENFOO) as {tt.fax} from TESTTABLE, OTHERTABLE where TESTTABLE.FOOBAR = OTHERTABLE.SOMECOLUMN"; // just FYI, in my real example there will be no POJO for OTHERTABLE
Query query = session.createSQLQuery(kysely).addEntity("tt", TestTable.class);
List list = query.list();
b) and also somewhere else like this:
Code:
TestTable tt = new TestTable();
... // set the values of the POJO
getHibernateTemplate().save(tt); // (Spring)
c) and also not native sql:
Code:
getHibernateTemplate().find("from TestTable tt where tt.fooBar > 1"); // (Spring)
the Questions:I have tried both 1. and 2., but none of them works with all of
a) b) and
c)...
I. Is there some way to just map an aliased column to a POJO, but when saving it, it would not be persisted. When I tried alternative 1.,
a) and
b) worked fine, but
c) failed, which leads to next question
II. Is there a way to just read
some of the database columns into
some of the POJO:s fields, for instance by only selecting TESTTABLE.FOOBAR into tt.fooBar? Doing it my way, it is quite cumbersome to map all fields from the pojo in the SELECT (when the pojo has lots of fields)
III. I was looking at the annotation @ColumnResult, but I'm a very new user of Hibernate, I'm not sure is this anything I could use.
IV. If it is hopeless to annotate the POJO, could anyone suggest how to do the whole thing better? :)
I appreciate any answers