-->
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.  [ 4 posts ] 
Author Message
 Post subject: breaking data integrity
PostPosted: Fri Feb 27, 2004 6:55 pm 
Newbie

Joined: Wed Feb 25, 2004 7:20 pm
Posts: 4
Hi!

I have two tables, Users and Events... The relationship is described as (one) Event know about which (one) User who registered the event. A user does not know what events it have registered. Looks like a one way directional one-to-one relationship, right?

But I figured i could get away with many-to-one by using the primary key from Users as a foreign key in Events. Am i on the right track?

Event.hbm.xml
Code:
    <class
        name="se.rullstol.ejb.persistence.Event"
        table="Events"
        dynamic-update="false"
        dynamic-insert="false">

        <many-to-one
            name="user"
            class="se.rullstol.ejb.persistence.User"
            cascade="all"
            outer-join="auto"
            update="true"
            insert="true"
            column="userId"/>
   </class>


User.hbm.xml

Code:
   
<class
        name="se.rullstol.ejb.persistence.User"
        table="Users"
        dynamic-update="false"
        dynamic-insert="false">

        <id name="id" column="Id" type="long">
            <generator class="identity">
            </generator>
        </id>
</class>


database:

Code:
table Users (
   Id BIGINT NOT NULL AUTO_INCREMENT,
   primary key (Id)
);
table Events (
   Id BIGINT NOT NULL AUTO_INCREMENT,
   userId BIGINT,
   primary key (Id)
);

alter table Events add index (userId), add constraint FK7C6CCD39CE2B2E46 foreign key (userId) references Users (Id);

Events
+----+--------+
| id | userid |
+----+--------+
|  1 |      1 |
|  2 |      1 |
|  3 |      2 |
+----+--------+

Users
+----+
| id |
+----+
|  1 |
|  2 |
+----+



This actually works to good. Add and Update, fine! Deleting users that have their FK in the Events table also seems ok to hibernate?? I thought that hibernate (2.1) would throw an excpetion since it will break the integrity of the tables... and since im using (the latest version) of MySQL, the database wont complain either.. here's the testcode:

Testcode:
Code:
User user = (User)session.load(User.class, new Long(1));
session.delete(user);
session.flush();


I've been reading, reading and trying, trying but nothing seems to work... one-to-one relationship (with property-ref) in the Events.hbm.xml doesnt seem to work either..

Thankful for any help

regards,
Kristoffer


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 9:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No, Hibernate does not (and /can/ not, in general) check to see if any other FKs refer to the object. That is up to the db to do.

Why not just use innodb tables??


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 28, 2004 7:49 am 
Newbie

Joined: Wed Feb 25, 2004 7:20 pm
Posts: 4
Thanks for the quick answer, at least now i *know* that it is not possible from hibernate.

I've read about innodb and it seems like MySQL 4.X uses it by default... but i still dont get an error from mysql.. maybe there is something wrong with the sql that hbm2ddl generates??

Code:
drop table if exists Users;
drop table if exists Events;
create table Users (
   Id BIGINT NOT NULL AUTO_INCREMENT,
   primary key (Id)
);
create table Events (
   Id BIGINT NOT NULL AUTO_INCREMENT,
   userId BIGINT,
   primary key (Id)
);
alter table Events add index (userId), add constraint FK7C6CCD39CE2B2E46 foreign key (userId) references Users (Id);


after reading in the mysql manual i see that for creating innodb tables you need to add a type=InnoDB like:

Code:
create table Events (
   Id BIGINT NOT NULL AUTO_INCREMENT,
   primary key (Id)
) TYPE = InnoDB;



Is it possible to generate this from hbm2dll.. or is there a way to tell mysql to only create tables of innodb type??

regards,
Kristoffer


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 28, 2004 7:52 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Search the forum, this has been extensively discussed lately.


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