-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to insert into a table without primary key
PostPosted: Wed Apr 20, 2005 1:21 pm 
Newbie

Joined: Wed Apr 20, 2005 1:06 pm
Posts: 2
Hi everyone,

I am recently working with Hibernate and have got a problem.

First I have got a table in postgresql created with a primary key as below.
CREATE TABLE SB_USER_ROLES(
userid INTEGER NOT NULL,
roleid INTEGER NOT NULL,
FOREIGN KEY (userid) REFERENCES SB_USERS(userid) ON DELETE CASCADE,
FOREIGN KEY (roleid) REFERENCES SB_ROLES(roleid) ON DELETE CASCADE
);

userid and roleid in table SB_USER_ROLES are not unique.

I created the mapping file as below.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping auto-import="true">
<class name="SBRole" table="SB_USER_ROLES">
<id name="id" type="long" column="oid">
<generator class="sequence">
<param name="sequence">oid</param>
</generator>
</id>

<property name="userid" type="integer">
<column name="USERID" not-null="true"/>
</property>

<property name="roleid" type="integer">
<column name="ROLEID" not-null="true"/>
</property>

</class>

And the SBRole class has get/setUserid/Roleid() methods.

There is no problem for me to query the database using the statement below:
Query q = session.createQuery("from SBRole");

But I could not insert rows into the table and the useful error message seems to be:
Caused by: org.postgresql.util.PSQLException: ERROR: relation "oid" does not exist
SBRole sbr1 = new SBRole();
sbr1.setUserid(10);
sbr1.setRoldid(10);
session.save(sbr1);
transaction.commit();

Could anybody let me know how to solve the problem? In fact I am afraid the <id> part of my mapping file is wrong. Thanks.

X. Yang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 5:22 am 
Beginner
Beginner

Joined: Wed Apr 21, 2004 8:33 am
Posts: 27
Hi
You dont have a column called OID so obviously you mapping file is wrong and thatswhy it is throwing that error. Just try genearting the class file and xml file using MiddleGen Tool. It should give you a clear picture

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 12:20 pm 
Newbie

Joined: Wed Apr 20, 2005 1:06 pm
Posts: 2
yvijaykumar wrote:
Hi
You dont have a column called OID so obviously you mapping file is wrong and thatswhy it is throwing that error. Just try genearting the class file and xml file using MiddleGen Tool. It should give you a clear picture

Thanks


Thank you for your reply. I tried MiddleGen in the past two days and made it work for normal table with a primary key. But still I have problem as described below.

Table 1, userid is the primary key
Table 2, roleid is the primary key
Table 3, userid and roleid are both foreign keys and that all in this table

I tried to use MiddleGen to generate mapping files for the above three tables and found composite-id should be used. Then when I tried to generate the java code, it was complained that composite-id is not correct. In fact it shouldn't be empty. I wonder how to set up the composite-id correctly. Also I want to know in this case, is it necessary to include Table 1 and 2 for MiddleGen. Thanks.

X. Yang


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>
<!--
    Created by the Middlegen Hibernate plugin 2.1

    http://boss.bekk.no/boss/middlegen/
    http://www.hibernate.org/
-->

<class
    name="sbdb.SbUserRole"
    table="sb_user_roles"
>

    <composite-id>
    </composite-id>

    <!-- Associations -->
    <!-- derived association(s) for compound key -->
    <!-- end of derived association(s) -->

    <!-- bi-directional many-to-one association to SbRole -->
    <many-to-one
        name="sbRole"
        class="sbdb.SbRole"
        not-null="true"
    >
        <column name="roleid" />
    </many-to-one>
    <!-- bi-directional many-to-one association to SbUser -->
    <many-to-one
        name="sbUser"
        class="sbdb.SbUser"
        not-null="true"
    >
        <column name="userid" />
    </many-to-one>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 12:35 pm 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
MiddleGen will only be able to infer database relations correctly if they are set up correctly in the first place - it can't work magic. It sounds like you have a table that has no primary key - is this correct?

You say:
Quote:
First I have got a table in postgresql created with a primary key as below.


... and then you go on to present DDL that defines no primary key, thus when MiddleGen reverse engineers your schema, it can't define any columns in the primary key, because there aren't any there!

So you are saying that it is possible to have multiple rows with exactly the same pair of (userid,roleid)? Or should this table have a primary key defined as a primary key on both the userid and roleid columns?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 3:30 am 
Beginner
Beginner

Joined: Wed Apr 21, 2004 8:33 am
Posts: 27
Make both the UserID and Role ID as primary key in the Third table , Then generate the mappings and class file It should work fine.


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