Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

two questions

 
camelia codarcea
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello to everybody, I have two unclear issues:

1. I have just found out that I have to use annotations instead of xml mapping.
I have looked in the net and saw examples like:

@Id @GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return this.id;
}

but, I have also found some source code that use the following approach:


/**
* @hibernate.property
* @hibernate.column name = "Content" not-null = "true" length = "25" index = "IX_Content"
* @return
*/
public String getContent() {
return content;
}

Can you tell me what is the difference and which one is better to use ?


2. The second question I have is about design: what can I do if I have a table with a lot of fields, but for some operations I will use only few fields ? Do I have to load the entire raw from the table and assign it to an object and then work with the data I need from it, or can I chose what fields to load from table in the DB?

Ex: MyEntity.java with fields: f1, f2, f3, f4, f5, f6, f7 which will
map to the table
MyTable with the fields: f1, f2, f3, f4, f5, f6, f7, but I have some operations that use only f1, f2, f3, other that use only f1, f4, f7, other use only f3, f4, f6, etc.
Do I have every time to load from the table the raw that contains all these 7 fields, or can I make some other related objects that contain only the fields I need and work with that object ?

thank you
 
camelia codarcea
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

I have found aout that tha first approach is with annotations and the second approach is with XDoclet.
Annotations are more elegant because they checked at compile time.


The second question is still open, the design question.

Thank you
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well there are two approaches, let Hibernate fill all 7 columns, and just use what you have mapped.

Or use projection queries, which are really just queries that have the Select portion state just which fields you want. But for projection queries you will want to make up classes that just have those fields. For instance



Then you HQL or JPAQL, will be

"Select new MyClass(t.column1, t.column4) From Table t"

This will return a list of MyClass with the data from just those two columns from the database table.

Have fun

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic