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.