-->
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.  [ 8 posts ] 
Author Message
 Post subject: Can I map an object to an old table?
PostPosted: Tue Jun 01, 2004 9:24 am 
Newbie

Joined: Tue Jun 01, 2004 9:11 am
Posts: 4
I want to use Hibernate with previously created java classes and database tables, so I assume (I am a beginer) I must create a mapping including those properties of the class I want to include in the database table.
The problem is when I try to save a new object Hibernate deletes the old table and creates a new one, instead of saving object properties in the previous table columns in a new register. I checked name correspondence between the mapping and table columns and is correct.
What am I doing wrong?
Can I use an Hibernate mapping between a class and a previous table (not created with Hibernate) or not?
Specifics about the class and the table seem irrelevant to me, but I can include one the several examples I have tried if you need it convenient.
Thanks in advance.
Antonio


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 01, 2004 11:23 am 
Regular
Regular

Joined: Wed May 12, 2004 3:03 am
Posts: 51
Location: France
Hi,

How do you want some help if you don't give the code.... sorry, but it's written with a red background when you write a new post, so...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 02, 2004 3:03 am 
Newbie

Joined: Tue Jun 01, 2004 9:11 am
Posts: 4
Even if I am grateful to get an answer, I think you might as well read what I wrote.
This is not a problem about a concrete mapping and a concrete class, but a question about what you can and cannot do with Hibernate (2.1 btw, as the red square asks) and any database (say MySQL).
The real question is: Can you map a previously created class (not using every property of the class in the mapping) to a previously created table (not using every column in the table)? Is a simple question, I only need a yes (and how) or a nope.
Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 02, 2004 3:15 am 
Regular
Regular

Joined: Wed May 12, 2004 3:03 am
Posts: 51
Location: France
Ho... I'm really sorry...

And, for your question, the answer is yes.

And, sorry again...


Top
 Profile  
 
 Post subject: Re: Can I map an object to an old table?
PostPosted: Wed Jun 02, 2004 3:16 am 
Regular
Regular

Joined: Wed May 12, 2004 3:03 am
Posts: 51
Location: France
anunez wrote:
The problem is when I try to save a new object Hibernate deletes the old table and creates a new one, instead of saving object properties in the previous table columns in a new register. I checked name correspondence between the mapping and table columns and is correct.
What am I doing wrong?


It was because of this I asked you the source code....


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 02, 2004 5:32 am 
Newbie

Joined: Tue Jun 01, 2004 9:11 am
Posts: 4
It seems it's my time to apologize.
So here is an example: (Hibernate 2.1, MySQL)
I have a class
public class Centro {
private String prop1;
private String prop2;
private String prop3;
// ... getters and setters
}

and a previous mapping not made with Hibernate to a table with
this structure:
table t_centro
- prop1 : VARCHAR
- prop2 : VARCHAR
- prop3 : VARCHAR

Now I try to do an Hibernate mapping not including prop2 and prop3, only with prop1:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="saci.Centro" table="t_centro">
<id type="int" column="id">
<generator class="native"/>
</id>
<property name="prop1" column="prop1" type="string" />
</class>
</hibernate-mapping>

When I use

Centro ctr1 = new Centro();
// setting, etc
session.save(ctr1);

I get my previous registers in this table erased in the database, and a new table is created only with id and prop1 columns.

Hibernate log4j says:
11:27:04,742 DEBUG SchemaExport:132 - drop table if exists t_centro
11:27:04,772 DEBUG SchemaExport:149 - create table t_centro (
id INTEGER NOT NULL AUTO_INCREMENT,
prop1 VARCHAR(255)
)
11:27:04,812 INFO SchemaExport:160 - schema export complete

and the end of the story is I lost my previous table and data!
How can I keep my old data while working with the new mapping?

Thanks again.
Antonio


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 02, 2004 5:39 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Remove hbm2ddl.auto from your configuration. And probably read up what the configuration options you are setting are doing.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 02, 2004 7:55 am 
Newbie

Joined: Tue Jun 01, 2004 9:11 am
Posts: 4
Ok, it solved it.
Thanks for your help, Charles and Michael.
Kind regards
Antonio


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.