Title: RE: [Hibernate] Parent Child and form-based authentication in Tom cat

 Where you are doing the logic to decide whether to save or update..  Wouldn't you want to do sess.saveOrUpdate(user)?

Eric

-----Original Message-----
From: Raible, Matt
To: '[EMAIL PROTECTED]'
Sent: 7/30/03 2:51 PM
Subject: RE: [Hibernate] Parent Child and form-based authentication in Tom cat

I tried linking to the object id fields, but then I realized (in
appfuse)
that it was matching the user's id with the user_role's id.  It just
happened that my data supported this.  To do the true parent-child
mapping,
I had to change the user's primary key to username and then I was able
to
fetch the user's roles.  The problem with using an assigned primary key
is
that I can't detect (as easily) when a new user has been added.  The
code
below works, except when I load/update/save a user's information in the
same
session.

        try {
            List users =
                ses.find("from u in class " + User.class
                         + " where u.username=?", user.getUsername(),
                         Hibernate.STRING);

            if (users.size() > 0) {
                ses.update(user);
            } else {
                ses.save(user);
            }
            ses.flush();
        } catch (Exception e) {
            throw new DAOException(e);
        }

When I load/update/delete, here's that I get:

        userForm = mgr.getUser(ses, "tomcat");
        userForm.setPhoneNumber("303-555-1212");

        if (log.isDebugEnabled()) {
            log.debug("saving user with updated phone number: " +
userForm);
        }
       
        userForm = mgr.saveUser(ses, userForm);
        assertTrue(userForm.getPhoneNumber().equals("303-555-1212"));

ERROR:

    [junit] Caused by: net.sf.hibernate.HibernateException: Another
object
was associated with this
id (the object with the given id was already loaded):
[org.appfuse.persistence.UserRole#1]
    [junit]     at
net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1294)
    [junit]     at
net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1218)

I don't have a <many-to-one> mapping in my UserRole class b/c basically,
I
don't see the point.  I'm never grabbing data from "user_role" without
fetching the user, then their roles first.  However, I'm having lots of
issues and I'm about to go back to a read-only mapping and updating the
roles myself - it just seems easier and I think users will understand it
better - especially since I've wasted the last day trying to do it the
"right way."

Matt

-----Original Message-----
From: Richard Mixon [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, July 30, 2003 11:35 AM
To: Raible, Matt
Subject: RE: [Hibernate] Parent Child and form-based authentication in
Tomcat


Matt,

I'm just using the "<set" in my example to fetch roles. I see two
differences between our examples:
1) You are using "<bag", I'm using "<set" with "unsorted=false".
2) I am linking using the object id fields, you are trying to use the
character field "username" to link.

I re-checked the documentation. Most of the examples use "<element", but
there are a few that use "<one-to-many" and the usage seems consistent
with
what we are doing.

Maybe someone else can enlighten us?

 - Richard

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of Raible,
Matt
Sent: Wednesday, July 30, 2003 9:12 AM
To: '[EMAIL PROTECTED]'
Subject: RE: [Hibernate] Parent Child and form-based authentication in
Tomcat


Thanks Richard.  I'm going back to the basics for this next question.
Regardless of inserting/updating/deleting roles.  How about just
fetching
child records.

Shouldn't the following create a link between the "app_user" table and
the
"user_role" table - joining on username?

    <class
        name="org.appfuse.persistence.User"
        table="app_user"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <bag
            name="roles"
            table="user_role"
            lazy="false"
            inverse="false"
            cascade="all"
        >

              <key
                  column="username"
              />

              <one-to-many
                  class="org.appfuse.persistence.UserRole"
              />
        </bag>

Thanks,

Matt

-----Original Message-----
From: Richard Mixon [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, July 29, 2003 8:30 PM
To: '[EMAIL PROTECTED]'
Subject: RE: [Hibernate] Parent Child and form-based authentication in
Tomcat


Matt,

Here's how I've got it mapped and it seems to work just fine. A little
wrinkle, in that my user class is a subclass of a person that is a
subclass
of something we call an "Associate" - any person or group of people that
can
play certain roles within the system. I've tried to remove the
superflous
stuff.

The other thing is that you need the actual "userid" and "rolename" in
the
user-roles table, not just the foreign key (ids). I have an exit in my
DAO
that maintains these fields when updates are made.

<hibernate-mapping>
    <class name="com.ltoj.persistence.base.PoAssociate"
table="PoAssociate"
dynamic-update="false" dynamic-insert="false" >
        <id name="id" column="id" type="long" unsaved-value="null" >
            <generator class="native"> </generator>
        </id>
        <version name="version" type="int" column="version" />
        <discriminator column="class" type="string" length="24" />
        <subclass name="com.ltoj.persistence.base.PoPerson"
            dynamic-update="false" discriminator-value="PoPerson" >
          <component name="personName"
class="com.ltoj.persistence.base.PoPersonName" >
            ...
          </component>
          <subclass name="com.ltoj.persistence.base.PoUser"
dynamic-update="false" discriminator-value="PoUser" >
            <property name="userid" type="java.lang.String"
update="true"
insert="true" column="userid" length="24" unique="true" />
            <property name="password" type="java.lang.String"
update="true"
insert="true" column="password" length="40" unique="false" />
            <property name="email" type="java.lang.String" update="true"
insert="true" column="email" length="60" unique="false" />
            <set name="userRoles" table="PoUserRole" lazy="false"
inverse="false" cascade="all" sort="unsorted" >
              <key column="userOwner" />
              <one-to-many class="com.ltoj.persistence.base.PoUserRole"
/>
            </set>
          </subclass>
        </subclass>

    </class>

    <class name="com.ltoj.persistence.base.PoUserRole"
        table="PoUserRole" dynamic-update="false" dynamic-insert="false"
>
        <id name="id" column="id" type="long" unsaved-value="null" >
          <generator class="native"> </generator>
        </id>
        <version name="version" type="int" column="version" />
        <many-to-one name="userOwner"
class="com.ltoj.persistence.base.PoUser" cascade="none"
outer-join="auto"
update="true" insert="true" >
          <column name="userOwner" unique-key="userkey" />
        </many-to-one>
        <many-to-one name="userRole"
class="com.ltoj.persistence.base.PoRole" cascade="none"
outer-join="auto"
update="true" insert="true" >
          <column name="userRole" unique-key="userkey" />
        </many-to-one>
        <property name="userid" type="java.lang.String" update="true"
insert="true" >
          <column name="userid" length="24" unique-key="authenticate" />
        </property>
        <property name="roleName" type="java.lang.String" update="true"
insert="true" >
          <column name="roleName" length="24" unique-key="authenticate"
/>
        </property>
    </class>

    <class name="com.ltoj.persistence.base.PoRole" table="PoRole"
dynamic-update="false" dynamic-insert="false" >
        <id name="id" column="id" type="long" unsaved-value="null" >
          <generator class="native"></generator>
        </id>
        <property name="name" type="java.lang.String" update="true"
insert="true" >
            <column name="name" unique-key="userkey" />
        </property>
    </class>

</hibernate-mapping>

HTH - Richard

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of Raible,
Matt
Sent: Tuesday, July 29, 2003 5:01 PM
To: '[EMAIL PROTECTED]'
Subject: [Hibernate] Parent Child and form-based authentication in
Tomcat


Sorry to post here, but SF forums are down.

I'm struggling (yet again) with a Parent/Child relationship.  Rather
than
describing my issues, I first want to make sure I'm mapping this
correctly.
In using a JDBCRealm (for authentication) in Tomcat, you must create a
"userTable", and a "userRole" table.  Here's a sample config from my
app-context.xml file:

<snip>
userTable="app_user" userNameCol="username" userCredCol="password"
       userRoleTable="user_role" roleNameCol="role_name"
</snip>

So here's a snip from my two tables:

mysql> desc app_user;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | bigint(20)   |      | PRI | NULL    | auto_increment |
| username     | varchar(255) |      | UNI |         |                |
| password     | varchar(255) |      |     |         |                |


mysql> desc user_role;
+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| id        | bigint(20)   |      | PRI | NULL    | auto_increment |
| username  | varchar(255) |      |     |         |                |
| role_name | varchar(255) |      |     |         |                |
+-----------+--------------+------+-----+---------+----------------+


Tomcat maps the username from the user_role table to match the username
column in the app_user table.  I shouldn't need the "id" in the
user_role
table, but I've found this easier with XDoclet since it can't generate
composite-ids.

Currently, I have the user_role table mapped as a one-to-many collection
on
User:

        <bag
            name="roles"
            lazy="true"
            inverse="false"
            cascade="all"
        >

              <key
                  column="id"
              />

              <one-to-many
                  class="org.appfuse.persistence.UserRole"
              />
        </bag>

I read Chapter 8 on Parent/Child and tried the recommended settings, but
couldn't get them to work (StackOverflow).

Since I think a lot of folks will be using Hibernate with Tomcat and a
JDBCRealm - I'd like to get the mapping figured out as a recommended
example.

Thanks,

Matt


-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01
/01
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel





-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01
/01
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel


-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01
/01
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel


-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01
/01
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to