-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to use "middlegenIDE" for tables without prima
PostPosted: Tue Oct 25, 2005 4:30 am 
Newbie

Joined: Tue Oct 11, 2005 3:55 am
Posts: 3
Hello. It contributes for the first time.
I can get the source code from DB schema by using "middlegenIDE" now.

That tool works well,
but the following condition without "primary-key"(Fig[1],Fig[2],Fig[3]),
My driver class(Fig[4]) can't insert records.

Exception is Fig[5].

It is unable to change schemas(add constraint primarykey) for system-migration.

What's wrong ?
Please let me know the answer.
Thanks

Fig[1]
Code:
CREATE TABLE group_info
(
  group_id int4 NOT NULL,
  group_name varchar(20) NOT NULL
)
WITH OIDS;
ALTER TABLE group_info OWNER TO postgres;
CREATE UNIQUE INDEX group_unq
  ON group_info
  USING btree
  (group_id);


Fig[2]
Code:
CREATE TABLE user_info
(
  user_id int4 NOT NULL,
  user_name varchar(20) NOT NULL
)
WITH OIDS;
ALTER TABLE user_info OWNER TO postgres;
CREATE UNIQUE INDEX user_unq
  ON user_info
  USING btree
  (user_id);


Fig[3]
Code:
CREATE TABLE relation_info
(
  group_id int4 NOT NULL,
  user_id int4 NOT NULL,
  "password" varchar(20) NOT NULL,
  CONSTRAINT group_fk FOREIGN KEY (group_id) REFERENCES group_info (group_id) ON UPDATE RESTRICT ON DELETE RESTRICT,
  CONSTRAINT user_fk FOREIGN KEY (user_id) REFERENCES user_info (user_id) ON UPDATE RESTRICT ON DELETE RESTRICT
)
WITH OIDS;
ALTER TABLE relation_info OWNER TO postgres;
CREATE UNIQUE INDEX relation_unq
  ON relation_info
  USING btree
  (group_id, user_id);


Fig[4]
Code:
package org.ultimania.driver;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.ultimania.model.GroupInfo;
import org.ultimania.model.RelationInfo;
import org.ultimania.model.UserInfo;

public class Driver {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Configuration config = new Configuration();
        config = config.configure();
        SessionFactory sessionFactory = config.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

        GroupInfo g1 = new GroupInfo();
        g1.setGroupId(new Integer(1));
        g1.setGroupName("group_1");

        GroupInfo g2 = new GroupInfo();
        g2.setGroupId(new Integer(2));
        g2.setGroupName("group_2");

        UserInfo u1 = new UserInfo();
        u1.setUserId(new Integer(1));
        u1.setUserName("user_1");

        UserInfo u2 = new UserInfo();
        u2.setUserId(new Integer(2));
        u2.setUserName("user_2");

        session.save(g1);
        session.save(g2);
        session.save(u1);
        session.save(u2);
        transaction.commit();

        RelationInfo r1 = new RelationInfo();
        r1.setGroupInfo(g1);
        r1.setUserInfo(u1);
        r1.setPassword("password_a");

        RelationInfo r2 = new RelationInfo();
        r2.setGroupInfo(g2);
        r2.setUserInfo(u2);
        r2.setPassword("password_b");

        RelationInfo r3 = new RelationInfo();
        r3.setGroupInfo(g2);
        r3.setUserInfo(u1);
        r3.setPassword("password_c");

        session.save(r1);
        session.save(r2);
        session.save(r3);
        transaction.commit();

        session.close();

    }

}



Fig[5]
Code:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.MappingException: Foreign key (FK1B110111A34439B1:relation_info [group_id])) must have same number of columns as the referenced primary key (group_info [group_id,group_name])
   at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:86)
   at org.hibernate.mapping.ForeignKey.setReferencedTable(ForeignKey.java:51)
   at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:976)
   at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:921)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:999)
   at org.ultimania.driver.Driver.main(Driver.java:21)


Environments are :
PostgreSQL 8(with PgAdmin3), Eclipse 3.1, MiddlegenIDE 1.3.2


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 8:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
The relationships need to be the same. No primar keys mean the system assume all fields are members of a compound key. Hence some confusion can occur on the relationsip constraint.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 26, 2005 10:18 pm 
Newbie

Joined: Tue Oct 11, 2005 3:55 am
Posts: 3
Thanks for your reply.

I found I've to rewrite *.hbm.xml and create classes for "primary key" as follows,
http://jroller.com/page/RickHigh?entry= ... mponent_of

Our database system for migrate, which has over 100 tables,
uses unique index instead of primary key and
cannot change schemas because of performance requirement.

There is no time to rewrite auto-generated xml files.
Is there good idea without rewriting?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 27, 2005 1:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
try to use hibernate tools where you via the reveng.xml can tell it which columns should be used as a primary key.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: [ FOR REFERENCE ]
PostPosted: Thu Oct 27, 2005 3:15 am 
Newbie

Joined: Tue Oct 11, 2005 3:55 am
Posts: 3
My doubt is solved, I'll report (see below).
That is almost scratched customizing.
I hope this tool to support the case table don't have primary key and
expect this to future release.


Thanks to all.

-----------------------------------------------------------------------------------
[ FOR REFERENCE ]
-----------------------------------------------------------------------------------


1. edit *.hbm.xml related to the table to which another table's foreign-key targets.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.2

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

<class
name="org.ultimania.model.GroupInfo"
table="group_info"
lazy="false"
>

<composite-id name="groupInfoPK" class="org.ultimania.pk.GroupInfoPK">
<key-property
name="groupId"
column="group_id"
type="int"
length="4"
/>
<!--
<key-property
name="groupName"
column="group_name"
type="java.lang.String"
length="20"
/> -->

</composite-id>

<!-- Customized -->
<property
name="groupName"
column="group_name"
type="java.lang.String"
length="20"
/>

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

<!-- bi-directional one-to-many association to RelationInfo -->

<set
name="relationInfos"
lazy="true"
inverse="true"
cascade="all"

>
<key>
<column name="group_id" />
</key>

<one-to-many
class="org.ultimania.model.RelationInfo"
/>
</set>

</class>
</hibernate-mapping>

-----------------------------------------------------------------------------------
2. create class file for primary key.

GroupInfoPK.java
Code:
package org.ultimania.pk;

import java.io.Serializable;

public class GroupInfoPK implements Serializable {

    private Integer groupId;
    public Integer getGroupId() {
        return groupId;
    }
    public void setGroupId(Integer id) {
        this.groupId = id;
    }

    public int hashCode() {
        return (groupId).hashCode();
    }
    public boolean equals(Object object) {
        if (this == object) {
            return true;
        }
        if (!(object instanceof GroupInfoPK)) {
            return false;
        }
        GroupInfoPK other = (GroupInfoPK) object;
        return this.groupId.equals(other.groupId);
    }
    public String toString() {
        return groupId.toString();
    }
}

-----------------------------------------------------------------------------------
3. edit the auto-generated class (change field which is defined as "unique index").

    package org.ultimania.model;

    import java.io.Serializable;
    import java.util.Set;
    import org.apache.commons.lang.builder.ToStringBuilder;
    import org.ultimania.pk.GroupInfoPK;


    /** @author Hibernate CodeGenerator */
    public class GroupInfo implements Serializable {

    /** identifier field */
    private GroupInfoPK groupInfoPK;

    /** identifier field */
    private String groupName;

    /** persistent field */
    private Set relationInfos;

    /** full constructor */
    public GroupInfo(GroupInfoPK pk, String groupName, Set relationInfos) {
    this.groupInfoPK = pk;
    this.groupName = groupName;
    this.relationInfos = relationInfos;
    }

    /** default constructor */
    public GroupInfo() {
    }

    public GroupInfoPK getGroupInfoPK() {
    return this.groupInfoPK;
    }

    public void setGroupInfoPK(GroupInfoPK pk) {
    this.groupInfoPK = pk;
    }

    public String getGroupName() {
    return this.groupName;
    }

    public void setGroupName(String groupName) {
    this.groupName = groupName;
    }

    public Set getRelationInfos() {
    return this.relationInfos;
    }

    public void setRelationInfos(Set relationInfos) {
    this.relationInfos = relationInfos;
    }

    public String toString() {
    return new ToStringBuilder(this)
    .append("groupId", getGroupInfoPK())
    .append("groupName", getGroupName())
    .toString();
    }

    }

-----------------------------------------------------------------------------------
4. an example to call

Code:
    public static void main(String[] args) {

        Configuration config = new Configuration();
        config = config.configure();
        SessionFactory sessionFactory = config.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

        GroupInfo g1 = new GroupInfo();
        GroupInfoPK gpk1 = new GroupInfoPK();
        gpk1.setGroupId(new Integer(1));
        g1.setGroupInfoPK(gpk1);
        g1.setGroupName("group_1");

        session.save(g1);

        transaction.commit();
        session.close();

    }


-----------------------------------------------------------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 29, 2005 2:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Sure a solution was to change the velocity script - the power of open source


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