Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Need some in Hibernate

 
   Database Help (Home) -> Java RSS
Next:  thread safe JDBC code  
Author Message
RC

External


Since: Mar 12, 2008
Posts: 4



(Msg. 1) Posted: Wed Mar 26, 2008 9:37 am
Post subject: Need some in Hibernate
Archived from groups: comp>lang>java>programmer, others (more info?)

Hello there,

I am new in Hibernate. I look at some examples in Hibernate 3.2
There is ALWAYS a Long id in a table class, like

public class State {
private Long id; // no such column in my STATE table in database
private String state_code; // two-character state code
private String state_name;

then six pairs of getXXX/setXXX();
}


My question is my existing table in database has NO id column.
I am NOT allow to altering the existed table.

How do I do the mapping (How do I re-write my XML mapping file)?
Thank Q very much in advance!

I try it as below XMl file, but I catch the error when I

try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}

Initial SessionFactory creation
failed.org.hibernate.PropertyNotFoundException: Could not find a getter
for state_code in class tables.State

-------------------------------------------------
State.hbm.xml

<hibernate-mapping>
<class name="tables.State" table="STATE">
<id name="state_code" type="string" column="STATE_CODE">
<generator class="native" />
</id>
<property name="state" type="string" column="STATE" />
</class>
</hibernate-mapping>

 >> Stay informed about: Need some in Hibernate 
Back to top
Login to vote
RC

External


Since: Mar 12, 2008
Posts: 4



(Msg. 2) Posted: Wed Mar 26, 2008 9:39 am
Post subject: Re: Need some in Hibernate [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I meant "Need some help"

 >> Stay informed about: Need some in Hibernate 
Back to top
Login to vote
Lew

External


Since: Feb 15, 2008
Posts: 43



(Msg. 3) Posted: Wed Mar 26, 2008 9:50 am
Post subject: Re: Need some in Hibernate [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

RC wrote:
> Hello there,
>
> I am new in Hibernate. I look at some examples in Hibernate 3.2
> There is ALWAYS a Long id in a table class, like
>
> public class State {
> private Long id; // no such column in my STATE table in database
> private String state_code; // two-character state code
> private String state_name;
>
> then six pairs of getXXX/setXXX();
> }
>
>
> My question is my existing table in database has NO id column.
> I am NOT allow to altering the existed table.
>
> How do I do the mapping (How do I re-write my XML mapping file)?
> Thank Q very much in advance!

Apply the name attribute of the id tag in the mapping table to whatever the
primary key actually is. By defining a generator, you are asking Hibernate to
generate the primary key for you, obviously not in accordance with the table's
design.
<http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-declaration-id>


If it's a multi-column key, you'd need
<http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-declaration-compositeid>


The Hibernate folks suggest:
> There is an alternative <composite-id> declaration to allow access to
> legacy data with composite keys. We strongly discourage its use for anything else.

I do not get what all this is about "legacy data" and "discourage its use".
It's quite common and proper for tables to have multi-column keys.

I haven't run across that editorial outlook in JPA articles (Java Persistence
API), for all that JPA is largely based on Hibernate.

--
Lew
 >> Stay informed about: Need some in Hibernate 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
To hibernate or not to hibernate? - Hello everyone, newbie here. I'm developing an application for a repair shop. Customer comes in with a product, gets registered and a receipt is printed out. I did it on Access five years ago and it's been working since then. But now I want to add a few....

Hibernate and log4j config under Tomcat. - "Under Tomcat 3.x and 4.x, you should place the log4j.properties under the WEB-INF/classes directory of your web-applications. Log4j will find the properties file and initialize itself. This is easy to do and it works." "The XML configu...

Hibernate + Tomcat - where to bind the session to? - Hello group, I am building a web application with Tomcat and Hibernate as O/R mapper. Now my question is where should I bind the session to? At the moment I have the Hibernate Session as a ThreadLocal, so for each thread a new HibernateSession is..

Struts/Hibernate and date troubles - Dear all, I'm getting squeezed between Struts and Hibernate when I use Date type for a record. My setup is the simplest possible. This is my mappings file <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC ..

Any discuss group about Hibernate, Spring? - Our office soon is going to replace JDBC with Hibernate. I am new to Hibernate. I try to follow the tutorial from Hibernate 3.2 doc/reference/ I am stop in chapter 1, wouldn't get the same results as the tutorial! BTW, what is the relationship between..
   Database Help (Home) -> Java All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]