hibernate newbie

W

warth33

Hi

Maybe I am at the wrong place? Its a question about hibernate. Can you
redirect me in that case?

Im a real newbie with hibernate.

Suppose you have the class Person

String givenname;
String lastname;
public void setGivenname(String str){givenname = str;}
public void setLastname(String str){lastname = str;}
public String getLastname(){return lastname;}
public String getGivenname(){return givenname;}

And you have some .hbm file that contains things like:

<property name="givenname" />
<property name="lastname" />

Right? it works more or less in this way?

Fine. Now, if I want the class Person be able to return completeName,
how can I accomplish this?

completeName is givenname + lastname.

Do I create a method inside Person the sets gets completeName? Do I
also need to modify .hbm file? Thank you very much in advance for
those that can help me.
 
J

JC

Hi

Maybe I am at the wrong place? Its a question about hibernate. Can you
redirect me in that case?

Maybe, but it's probably no big deal. Other places you can find help:

- http://forum.hibernate.org/ contains the official Hibernate user
help forum.
- IRC #hibernate on Freenode, but it's a bit quiet.
- IRC ##java on Freenode, sometimes off topic, you may or may not be
met with a friendly response there, but it will be friendly more often
than not.

Im a real newbie with hibernate.

Suppose you have the class Person

String givenname;
String lastname;
public void setGivenname(String str){givenname = str;}
public void setLastname(String str){lastname = str;}
public String getLastname(){return lastname;}
public String getGivenname(){return givenname;}

And you have some .hbm file that contains things like:

<property name="givenname" />
<property name="lastname" />

Right? it works more or less in this way?

Pretty much. There are other attributes to "property" elements (such
as "type") but Hibernate generally uses sane defaults that in most
cases Do The Right Thing (TM).

Fine. Now, if I want the class Person be able to return completeName,
how can I accomplish this?

completeName is givenname + lastname.

Do I create a method inside Person the sets gets completeName? Do I
also need to modify .hbm file? Thank you very much in advance for
those that can help me.

The easiest way is to simply add a method inside person that builds
the complete name from the first and last name. This does not affect
Hibernate at all. Hibernate does not care about other fields and
members in your class that are not described in the .hbm file. So:

public class Person {
...
String givenname;
String lastname;
public void setGivenname(String str){givenname = str;}
public void setLastname(String str){lastname = str;}
public String getLastname(){return lastname;}
public String getGivenname(){return givenname;}
...
public String getCompletename(){return givenname + lastname;}
...
}

Will not interfere with Hibernate, and your entity definition still
only contains:

<property name="givenname" />
<property name="lastname" />



HTH,
Jason
 
L

Lew

JC said:
public String getCompletename(){return givenname + lastname;}

You might want to slip a space in between there.

A strict interpretation of Java coding conventions might call for the word
part "Name" to be capitalized:

return givenName +" "+ lastName;

You might run into some trouble with people that don't have two name parts,
e.g, "Cher".

If you get seriously into conventions for human names, you might not want to
put such business logic in the entity class at all, but factor it out into the
presentation or business logic layer. For example, internationalization
considerations would indicate a need for "familyName givenName" as a format,
or "familyName, givenName". Rather than clutter up the entity class with all
that, it's better to do that in another component.

Entity classes should live pretty close to the data layer, and the data layer
is no place for business or presentation logic.

Hibernate also supports the JPA annotation style of entity class, not
surprisingly since Hibernate had a lot to do with the formation of the JPA
standard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top