-->
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: Hibernate basic associate
PostPosted: Thu Jun 07, 2007 1:30 am 
Newbie

Joined: Mon Jan 16, 2006 11:36 pm
Posts: 4
I'm n00b at hibernate and I'm finding there are many ways of setting this up. I'm trying to use hibernate with JSF. I have a basic association going but would like to check that I've set this association up correctly.

I have two tables
Table: CatUser has fields
catuserid serial Primary key
username String
fname String
lname String
securityid int

Table: Security has fields
securityid serial Primary key
security String

Relationships
MANY CatUser has ONE Security Level
ONE Security Level has MANY CatUsers

Here is my mapping
Code:
<?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>
    <class name="pdfcat.model.CatUserBean" table="CATUSER">

        <id name="id" column="catuserid" type="integer">
            <generator class="native">
                <param name="sequence">catuser_catuserid_seq</param>
            </generator>
        </id>

        <property name="username"/>
        <property name="fname"/>
        <property name="lname"/>

        <many-to-one
            name="security"
            column="SECURITYID"
            class="pdfcat.model.SecurityBean"
            not-null="true" />
           
       
    </class>
</hibernate-mapping>



Here is my CatUserBean
Code:
/*
* CatUserBean.java
*
* Created on June 5, 2007, 3:10 PM
*
* Description:
*
*/
package pdfcat.model;

public class CatUserBean{

    private int id;

    private String username;
    private String fname;
    private String lname;
   
    private SecurityBean security;      // Many 'Users' have one 'security level'
    private String securitylvl;
   
    public int getId(){
      return id;
    }

    private void setId(int id) {
        this.id = id;
    }

    public String getUsername(){
            return username;   
    }

    public void setUsername(String username){
            this.username = username;
    }
   
    public String getFname(){
        return fname;
    }
   
    public void setFname(String fname){
        this.fname = fname;
    }
   
    public String getLname(){
        return lname;
    }
   
    public void setLname(String lname){
        this.lname = lname;
    }

    public SecurityBean getSecurity(){
        return security;
    }

    public void setSecurity(SecurityBean security){
        this.security=security;
    }

    public String getSecuritylvl(){
        return security.getSecurity();
    }

    public void setSecuritylvl(String securitylvl){
        this.securitylvl = securitylvl;
    }
}


All I want to do is display a table of Users from table 'CatUser' with their Security Name, instead of Securityid.

So I've created a little securitylvl variable to retrieve the security name, is this the correct way of going about this?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 07, 2007 12:35 pm 
Newbie

Joined: Wed May 30, 2007 1:30 pm
Posts: 3
Location: Salt Lake City, UT
Sorry, but I don't understand what your asking. Where is the "security name"? What is the purpose of the security level parameter? Are you saying that you want to retrieve a list of users based on their security level?


Top
 Profile  
 
 Post subject: Re: Hibernate basic associate
PostPosted: Fri Jun 08, 2007 10:03 pm 
Newbie

Joined: Tue Dec 05, 2006 7:40 am
Posts: 14
map7 wrote:
I'm n00b at hibernate and I'm finding there are many ways of setting this up. I'm trying to use hibernate with JSF. I have a basic association going but would like to check that I've set this association up correctly.

I have two tables
Table: CatUser has fields
catuserid serial Primary key
username String
fname String
lname String
securityid int

Table: Security has fields
securityid serial Primary key
security String

Relationships
MANY CatUser has ONE Security Level
ONE Security Level has MANY CatUsers

Here is my mapping
Code:
<?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>
    <class name="pdfcat.model.CatUserBean" table="CATUSER">

        <id name="id" column="catuserid" type="integer">
            <generator class="native">
                <param name="sequence">catuser_catuserid_seq</param>
            </generator>
        </id>

        <property name="username"/>
        <property name="fname"/>
        <property name="lname"/>

        <many-to-one
            name="security"
            column="SECURITYID"
            class="pdfcat.model.SecurityBean"
            not-null="true" />
           
       
    </class>
</hibernate-mapping>



Here is my CatUserBean
Code:
/*
* CatUserBean.java
*
* Created on June 5, 2007, 3:10 PM
*
* Description:
*
*/
package pdfcat.model;

public class CatUserBean{

    private int id;

    private String username;
    private String fname;
    private String lname;
   
    private SecurityBean security;      // Many 'Users' have one 'security level'
    private String securitylvl;
   
    public int getId(){
      return id;
    }

    private void setId(int id) {
        this.id = id;
    }

    public String getUsername(){
            return username;   
    }

    public void setUsername(String username){
            this.username = username;
    }
   
    public String getFname(){
        return fname;
    }
   
    public void setFname(String fname){
        this.fname = fname;
    }
   
    public String getLname(){
        return lname;
    }
   
    public void setLname(String lname){
        this.lname = lname;
    }

    public SecurityBean getSecurity(){
        return security;
    }

    public void setSecurity(SecurityBean security){
        this.security=security;
    }

    public String getSecuritylvl(){
        return security.getSecurity();
    }

    public void setSecuritylvl(String securitylvl){
        this.securitylvl = securitylvl;
    }
}


All I want to do is display a table of Users from table 'CatUser' with their Security Name, instead of Securityid.

So I've created a little securitylvl variable to retrieve the security name, is this the correct way of going about this?



Hum... if I understand correct... the answer is no. the follow a example one way make it:

Code:
<?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>
   <class name="pdfcat.model.CatUserBean" table="CATUSER">
   
      <id name="catuserid" column="catuserid">
         <generator class="native" />
      </id>
      
      <property name="username" column="username"></property>
      <property name="fname" column="fname"></property>
      <property name="lname" column="lname"></property>
      
      <many-to-one name="securityBean" class="pdfcat.model.SecurityBean" >
      
         <column name="securityId" not-null="true"   ></column>
         
      </many-to-one>
      
   </class>
</hibernate-mapping>   

Code:
<?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>
   <class name="pdfcat.model.SecurityBean">
      <id name="securityId">
         <generator class="native"></generator>
      </id>
      <property name="security"></property>
   </class>
</hibernate-mapping>

Code:
package pdfcat.model;
public class CatUserBean {
   
   private int catuserid;
   private SecurityBean securityBean;
   private String username;
   private String fname;
   private String lname;
//set and gets....
}
public class SecurityBean {
   
   private int securityId;
   private String    security;
//set and gets...
}   


here main:
Code:
package pdfcat.main;
import java.util.List;

import pdfcat.util.*;
import pdfcat.model.*;
import org.hibernate.*;
public class Main {

   public static void main(String[] args) {
      
      addSomeData();
      listUsers();

   }
   
   public static void listUsers() {
      
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      
      session.beginTransaction();
      
      Query q = session.createQuery("from CatUserBean");
      
      List<CatUserBean> beans = q.list();
      
      for(CatUserBean b: beans) {
         
         System.out.println(b.getFname()+" "+b.getLname()+" - "+b.getSecurityBean().getSecurity());
         
      }
      
      session.getTransaction().commit();      
      
   }

   private static void addSomeData() {
      
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      
      session.beginTransaction();
      
      CatUserBean catUserBean = new CatUserBean();
      
      catUserBean.setFname("Alex");
      catUserBean.setLname("Opa");
      catUserBean.setUsername("ualex");
      
      SecurityBean securityBean = new SecurityBean();
      
      securityBean.setSecurity("Admin");
      
      session.save(securityBean);
      
      catUserBean.setSecurityBean(securityBean);
      
      session.save(catUserBean);
      
      session.getTransaction().commit();
   }

}


ok ? it too is example many-to-one map.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 11, 2007 9:52 pm 
Newbie

Joined: Mon Jan 16, 2006 11:36 pm
Posts: 4
Thanks ualex.

That's exactly what I was trying to do. I had it working in that round-about way, but I want to make sure I'm doing everything the correct way.

I'm using Java Source 1.4 not 1.5 so I had to use this instead:
Code:

    public List getListCatUsers(){
        System.out.println("testCatUser");

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

   List result = session.createQuery("from CatUserBean").list();

        for(int i=0; i < result.size(); i++){
            System.out.println("int i = " + i);
           
            // Set the current bean to look at.
            CatUserBean catuserbean = (CatUserBean) result.get(i);
           
            // Display current bean.
            System.out.println(catuserbean.getFname() + " " + catuserbean.getLname() + " - " + catuserbean.getSecurityBean().getSecurity());
        }
       
        session.getTransaction().commit();
       
   return result;
    }


Is this correct for Java 1.4?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 12, 2007 1:00 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

Don't use session.beginTransaction(); and session.getTransaction().commit(); when there are no save or update or delete things.


Amila

(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 12, 2007 7:16 pm 
Newbie

Joined: Mon Jan 16, 2006 11:36 pm
Posts: 4
If I remove the 'session.beginTransaction();' line I get this error:

Code:
org.apache.jasper.JasperException: javax.servlet.jsp.JspException: javax.faces.el.EvaluationException: Error getting property 'listCats' from bean of type pdfcat.backing.Cat: org.hibernate.HibernateException: createQuery is not valid without active transaction
   org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
   org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
   org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
   org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
   com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:322)
   com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:130)
   com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
   com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
   com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:117)
   javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)
   org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)


Removing the 'session.getTransaction().commit();' line I can understand and and my getListCats() method works fine without it. Thanks.


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.