2

Is there a way I can map a field in an hibernate object to be loaded with a table query?

As an example lets say Table_Message has fields id(int),message_key(varchar),message_content(Clob),language(varchar). This table will hold messages in different languages(locale).

And another table thats mapped to an entity using hibernate. Comments with fields id(int),comment_message_id(varchar),created_date(datetime). comment_message_id refers to Table_Message's message_key column.

EDIT: Table_Message is NOT a mapped Entity in hibernate

Assuming my comment class is

public class Comment
{
   int id;
   String message;
   Date createdDate;
}

Is there a way to tell hibernate to load message by joining Comment table and Table_Message table by message_key with a default locale (for example 'en').

Basically is there a way to tell hibernate to load a field by running a specific query? And if so what is that way?

I know about writing a Custom SQL query for loading the entity. But since I'm using XDoclet there doesn't seem to be a way to do that. Also it will be very convenient if there's a way to do that for a single field.

Thihara
  • 7,031
  • 2
  • 29
  • 56

2 Answers2

1

You must join the tables by comment_message_id with message_key and further filter the result by language. I assume the message_key is unique.

As a side notice: you should use integer keys to have better performance.

You can try to write a database view in SQL and create an entity to opaque the view:

CREATE VIEW Comment_Table_Message AS 
SELECT c.id, c.comment_message_id, c.created_date, m.id AS mid, 
       m.message_content, m.language 
FROM Comment c, Table_Message m 
WHERE c.comment_message_id = t.message_key;

Now you can create an entity CommentTableMessage and use JPQL to filter results by language:

SELECT x FROM CommentTableMessage x WHERE x.language=?1

If Table_Message was a Hibernate entity you would write (in JPA terms):

@Entity
public class Comment
{
   int id;
   @ManyToOne()
   @JoinColumn(name="comment_message_id")
   TableMessage tableMessage;
   String message;
   Date createdDate;
}

@Entity
public class TableMessage {
   int id;
   @Id
   String messageKey;
   bytes[] messageContent; //I don't know how you want to deal with Blobs?
   String language;
}

Having that you can write a simple JPA Query: (Can you use JPA ? - next assumption)

SELECT c FROM Comment c WHERE c.tableMessage.language=?1
digital_infinity
  • 534
  • 6
  • 20
  • The thing is TableMessage is not mapped in hibernate. So I don't think this will work.... I think I didn't make it clear in the question. I will edit now. – Thihara May 30 '12 at 12:33
  • I edited the answer. New solution assumes that you have permissions to create new views in database. In some situation a programmer cannot change the database structure (eg. just before program release this kind of changes are not allowed) – digital_infinity May 30 '12 at 13:52
  • Yeah I also thought about creating a view but that is not going to work for me. I'm really looking for an specific solution that I stated or something similar without any database modification. – Thihara May 30 '12 at 14:09
  • How can I write a JPA query on a table not mapped to any entity? – Thihara May 31 '12 at 03:30
  • I see you wrote it, but do you know that you can map automatically SQL NativeQuery results to an entity ? – digital_infinity May 31 '12 at 07:30
  • The answer for [ResultTransformer usage](http://stackoverflow.com/a/7042443/1326149) was not using `ResultTransformer` at all. – digital_infinity May 31 '12 at 07:38
1

I guess ResultTransformer may help you in this. Please check

http://docs.jboss.org/hibernate/orm/3.3/api/org/hibernate/transform/ResultTransformer.html http://stackoverflow.com/questions/6423948/resulttransformer-in-hibernate-return-null

Aryan
  • 1,767
  • 2
  • 22
  • 39