facebook

Help: Noob Question.

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #270903 Reply

    Alexk400
    Member

    Hello experts,

    I just downloade myeclipse to test hibernate mapping.

    Created two simple table in mysql and did reverse engineering.

    Let me type the tabel info.

    Mysql dump for users table.

    /*
    MySQL Data Transfer
    Source Host: localhost
    Source Database: test
    Target Host: localhost
    Target Database: test
    Date: 5/30/2007 11:39:32 PM
    */

    SET FOREIGN_KEY_CHECKS=0;
    — —————————-
    — Table structure for users
    — —————————-
    CREATE TABLE `users` (
    `uid` int(11) NOT NULL,
    `age` int(11) default NULL,
    `firstname` varchar(20) default NULL,
    `lastname` varchar(20) default NULL,
    PRIMARY KEY (`uid`),
    KEY `uid` (`uid`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    — —————————-
    — Records
    — —————————-
    INSERT INTO `users` VALUES (‘1′, ’30’, ‘Bruce”, ‘Lee’);
    INSERT INTO `users` VALUES (‘2’, ‘100’, ‘John ‘, ‘Wayne’);
    INSERT INTO `users` VALUES (‘3′, ’33’, ‘Frank’, ‘Wong’);

    Mysql dumb for users_emails

    /*
    MySQL Data Transfer
    Source Host: localhost
    Source Database: test
    Target Host: localhost
    Target Database: test
    Date: 5/30/2007 11:39:43 PM
    */

    SET FOREIGN_KEY_CHECKS=0;
    — —————————-
    — Table structure for users_emails
    — —————————-
    CREATE TABLE `users_emails` (
    `id` int(11) NOT NULL,
    `user_uid` int(11) NOT NULL,
    `email` varchar(30) default NULL,
    PRIMARY KEY (`id`,`user_uid`),
    KEY `user_uid` (`user_uid`),
    CONSTRAINT `users_emails_ibfk_1` FOREIGN KEY (`user_uid`) REFERENCES `users` (`uid`) ON DELETE NO ACTION ON UPDATE NO ACTION
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    — —————————-
    — Records
    — —————————-
    INSERT INTO `users_emails` VALUES (‘1’, ‘1’, ‘johnwayne@hotmail.com’);

    ================================================================

    I used reverse engineering on these tables and created POJOS.

    In usersemail table id and users id are composite keys. In composite keys situation i had assign primary keys for useremail table.

    My problem is when i add email entry to users table by adding useremail object to users , can i insert the data into usersemail table?.

    Atpresent i had to explicit save on useremails object. I want user object recognize any useremail object added to its set automatically.

    Can someone help me with these?.

    My crap code:

    private void addEmail(Integer userId, String email) {
    try {
    //Session session = HibernateUtil.currentSession();
    Transaction tx = session.beginTransaction();

    Users user = (Users) session.load(Users.class, userId);
    UsersEmailsId myId=new UsersEmailsId(new Integer(1),user.getUid());
    // UsersEmails emails = (UsersEmails) session.load(UsersEmails.class, myId);

    UsersEmails emails = new UsersEmails(myId,user,email);

    user.getUsersEmailses().add(emails);
    //session.save(emails);

    tx.commit();
    hsqlCleanup(session);
    //HibernateUtil.closeSession();
    tearDown();
    } catch (HibernateException e) {
    throw new RuntimeException(e);
    }
    catch (Exception e) {
    throw new RuntimeException(e.getMessage());
    }
    }

    Above code does not insert without next line i commented ( session.save(emails); )

    Here is my useremails.hbm.xml

    <?xml version=”1.0″ encoding=”utf-8″?>
    <!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
    http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”&gt;
    <!–
    Mapping file autogenerated by MyEclipse Persistence Tools
    –>
    <hibernate-mapping>
    <class name=”EventOrganizer.UsersEmails” table=”users_emails” catalog=”test”>
    <composite-id name=”id” class=”EventOrganizer.UsersEmailsId”>
    <key-property name=”id” type=”java.lang.Integer”>
    <column name=”id” />
    </key-property>
    <key-property name=”userUid” type=”java.lang.Integer”>
    <column name=”user_uid” />
    </key-property>
    </composite-id>
    <many-to-one name=”users” class=”EventOrganizer.Users” update=”false” insert=”false” fetch=”select”>
    <column name=”user_uid” not-null=”true” />
    </many-to-one>
    <property name=”email” type=”java.lang.String”>
    <column name=”email” length=”30″ />
    </property>
    </class>
    </hibernate-mapping>

    Please email me if you can help me with these ?,

    Also if any of experts have do’s and don’ts for hibernate mapping rules?.

    Your help is very much appreciated. Please email me at pokerking400@hotmail.com or respond here.

    Thanks
    alexk

    #270949 Reply

    Riyad Kalla
    Member

    Alexk,
    You need to the save command so Hibernate knows to attach the object to the session. You could try asking your quesiton in the Hibernate forums to see if one of the gurus knows a cool way to avoid that… but I personally don’t.

Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: Help: Noob Question.

You must be logged in to post in the forum log in