-->
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.  [ 13 posts ] 
Author Message
 Post subject: Hibernate replacing my tables
PostPosted: Thu Nov 04, 2004 1:42 pm 
Newbie

Joined: Mon Sep 27, 2004 3:01 am
Posts: 17
Hi,

Why is Hibernate replacing existing tables in the database with its own, different tables upon a call to session.find("from X")?

Here's my mapping document:
Code:
    <class name="User" table="tdb_user">
        <id name="id" type="string" column="id">
            <generator class="uuid.hex"/>
        </id>
        <property name="username" column="username" type="string"/>
        <property name="realname" column="realname" type="string"/>
    </class>

Here's the class I'm mapping:
Code:
public class User {
   
    private String id;

    private String username;

    private String realname;

    public User() {
    }
   
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getRealname() {
        return realname;
    }

    public void setRealname(String name) {
        this.realname = name;
    }

}

And this is what I used for creating the table before triggering the SELECT from within Tomcat:
Code:
create table tdb_user (
  id VARCHAR(32) PRIMARY KEY,
  username VARCHAR(32) NOT NULL UNIQUE,
  password VARCHAR(32) DEFAULT NULL,
  realname VARCHAR(100) NOT NULL
) TYPE=InnoDB;


Note that I've included a 'password' column in the database and that isn't mapped to the User class.

Finally, this is what happens when Hibernate has had a chance to do the SELECT:
Code:
mysql> describe tdb_user;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | varchar(32)  |      | PRI |         |       |
| username | varchar(32)  |      | UNI |         |       |
| password | varchar(32)  | YES  |     | NULL    |       |
| realname | varchar(100) |      |     |         |       |
+----------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql>
mysql> describe tdb_user;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | varchar(255) |      | PRI |         |       |
| username | varchar(255) | YES  |     | NULL    |       |
| realname | varchar(255) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql>


I'd really appreciate any pointers towards where I'm going to the woods.

I'm using Hibernate 2.1.6 and MySQL 4.0.22.

-Lasse-


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 10:30 am 
Newbie

Joined: Mon Sep 27, 2004 3:01 am
Posts: 17
Anyone?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 10:32 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
show hibernate.hbm.xml

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 10:53 am 
Newbie

Joined: Mon Sep 27, 2004 3:01 am
Posts: 17
Thanks for looking into this.

Here's my hibernate.cfg.xml:
Code:
<!DOCTYPE hibernate-configuration PUBLIC
   "-//Hibernate/Hibernate Configuration DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>
   <session-factory>
      <property name="show_sql">true</property>
      <mapping resource="com/domain/User.hbm.xml"/>
   </session-factory>
</hibernate-configuration>


And here's the User.hbm.xml:
Code:
<?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="com.domain.User" table="tdb_user">
        <id name="id" type="string" column="id">
            <generator class="uuid.hex"/>
        </id>
        <discriminator column="usertype" type="string"/>
        <property name="username" column="username" type="string"/>
        <property name="realName" column="realname" type="string"/>
    </class>

</hibernate-mapping>


What I forgot to mention is that I'm using a user-provided connection, i.e. the Java code creates a Session as follows:
Code:
// this is called once per application
sessionFactory = new Configuration().configure().buildSessionFactory();

// this is called once per request
session = sessionFactory.openSession(connection);


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 11:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you probably have "hibernate.hbm2ddl.auto" set to something enabling automatic schemacreation... ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 11:05 am 
Newbie

Joined: Mon Sep 27, 2004 3:01 am
Posts: 17
max wrote:
you probably have "hibernate.hbm2ddl.auto" set to something enabling automatic schemacreation... ?

Umm... No. Unless it's "true" by default. I'll try to explicitly set it to false and report back.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 11:22 am 
Newbie

Joined: Mon Sep 27, 2004 3:01 am
Posts: 17
Heck. It seemed to work. I added
Code:
<property name="hbm2ddl.auto">off</property>

which seemed to get rid of the problem (even though "off" is not a valid value for that property according to the docs...).

Is this the way it's supposed to work?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 11:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
check if you have a hibernate.properties file somewhere in your classpath (including inside .jars) that set this setting otherwise ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 11:55 am 
Newbie

Joined: Mon Sep 27, 2004 3:01 am
Posts: 17
max wrote:
check if you have a hibernate.properties file somewhere in your classpath (including inside .jars) that set this setting otherwise ?

Ah. There it was. The default hibernate.properties file included this block:
Code:
## auto schema export

#hibernate.hbm2ddl.auto create-drop
hibernate.hbm2ddl.auto create
#hibernate.hbm2ddl.auto update

which obviously caused the behavior I saw, until I overrode it with .cfg.xml.

I definitely don't want create-drop and probably not create either (I like to fail fast...), but what does the "update" option do?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 12:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
that setting is not so in the hibernate releases (maybe it was that once, but it was quickly fixed). ... are you using the latest release or have you modified that file by your own ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 12:35 pm 
Newbie

Joined: Mon Sep 27, 2004 3:01 am
Posts: 17
max wrote:
that setting is not so in the hibernate releases (maybe it was that once, but it was quickly fixed). ... are you using the latest release or have you modified that file by your own ?

I haven't modified it myself -- I just copied it from somewhere, probably the "adminapp" sample application sources.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 12:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
lkoskela wrote:
max wrote:
that setting is not so in the hibernate releases (maybe it was that once, but it was quickly fixed). ... are you using the latest release or have you modified that file by your own ?

I haven't modified it myself -- I just copied it from somewhere, probably the "adminapp" sample application sources.


aah .... copy-pasted code/settings! Dangerous ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 1:00 pm 
Newbie

Joined: Mon Sep 27, 2004 3:01 am
Posts: 17
max wrote:
aah .... copy-pasted code/settings! Dangerous ;)

You can say that again...


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