-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Native SQL: How to annotate POJOs correctly?
PostPosted: Wed May 06, 2009 7:02 am 
Newbie

Joined: Wed May 06, 2009 6:02 am
Posts: 2
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


Top
 Profile  
 
 Post subject: Re: Native SQL: How to annotate POJOs correctly?
PostPosted: Thu May 07, 2009 5:16 am 
Newbie

Joined: Wed May 06, 2009 6:02 am
Posts: 2
no answers?

Well, is there any possibility to annotate my previously named bean (without using transient for the field property), so that this won't result in hibernate exceptions?

HibernateUtils.find("from TestTable tt where tt.fooBar > 1");

I mean, it looks like that when fetching everything like this, it uses the bean's fields to map the columns... But in some cases I use native sql, filling up one of the fields with temporary/aliased columns, like in my example.

I would want some kind of Annotation like this:

@NeverAutomaticallyFetchFromDatabase


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.