-->
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.  [ 7 posts ] 
Author Message
 Post subject: problem with parent/child
PostPosted: Mon Mar 19, 2007 1:14 pm 
Newbie

Joined: Wed Mar 14, 2007 4:35 am
Posts: 7
Hi everybody

I have problem with hibernate and parent/child class. This is my .hbm.xml file.

<hibernate-mapping package="security.model">
<class name="Group" catalog="seguridad" table="gro_group">
<id name="id" column="gro_id">
<generator class="increment"/>
</id>
<property name="name" column="gro_name"/>
<property name="description" column="gro_description"/>
<many-to-one name="father" class="com.prisacom.se.security.model.Group" column="gro_father_id"/>

<set name="children">
<key>
<column name="gro_father_id"></column>
</key>
<one-to-many class="com.prisacom.se.security.model.Group"/>
</set>

<set name="permissions" inverse="true">
<key>
<column name="gro_id"></column>
</key>
<one-to-many class="com.prisacom.se.security.model.Permission"/>
</set>
</class>
</hibernate-mapping>




and this is my java class

public class Group extends ESElement {

private List users;

private Group father;

private Set children;

private Set permissions;

public Group() {
super();
}

public void addChild(Group child) throws DAOException {
// TODO: implementar
if (child==null){
throw new DAOException("Error al añadir un hijo al grupo, child es null");
}else {
if (children==null){
children= new HashSet();
}
this.children.add(child);
}
}

public void addUser(User user) throws DAOException {
// TODO: implementar
if (user==null){
throw new DAOException("Error al añadir un usuario al grupo, user es null");
}else {
if (users==null){
users= new ArrayList();
}
this.users.add(user);
}
}

public void removeChild(Group child) throws DAOException {
// TODO: implementar
if (child==null || children==null){
throw new DAOException("Error al eliminar un hijo del grupo");
} else {
this.children.remove(child);
}
}

public void removeUser(User user) throws DAOException {
// TODO: implementar
if (user==null || users==null){
throw new DAOException("Error al eliminar un usuario del grupo");
}else{
this.users.remove(user);
}
}

public Set getChildren() {
return this.children;
}

public void setChildren(Set children) {
this.children = children;
}
public Group getFather() {
return this.father;
}

public void setFather(Group father) {
this.father = father;
}

public Set getPermissions() {
return this.permissions;
}

private void setPermissions(Set permissions) {
// TODO: sólo para hibernate ¿funciona?
this.permissions = permissions;
}

public List getUsers() {
return this.users;
}

private void setUsers(List users) {
// TODO: sólo para hibernate ¿funciona?
this.users = users;
}

My problem is this mapping is wrong. I want to do a sql query like "where id=72" but with this problem, I have a "where (1=1)" query.

Anybody can help me? any idea?

thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 19, 2007 3:31 pm 
Beginner
Beginner

Joined: Thu Mar 01, 2007 9:40 am
Posts: 23
Location: UK
Hi,

Can't you say:
Code:
Group group = (Group) session.load(Group.class, "72");


Or use an HQL like:
"from Group g where g.id = ?"

Are you getting any exception when running hibernate?

--
Sam.

_________________
Sam.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 20, 2007 4:34 am 
Newbie

Joined: Wed Mar 14, 2007 4:35 am
Posts: 7
Hi Jikes2005. I'm not getting any exception. I have this sentence in my console:

select this_.gro_id as gro1_6_0_, this_.gro_name as gro2_6_0_, this_.gro_description as gro3_6_0_, this_.gro_father_id as gro4_6_0_ from prisaseguridad.gro_group this_ where (1=1)

And I want to change this sentence with this another

select this_.gro_id as gro1_6_0_, this_.gro_name as gro2_6_0_, this_.gro_description as gro3_6_0_, this_.gro_father_id as gro4_6_0_ from prisaseguridad.gro_group this_ where this_.gro_id = ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 20, 2007 6:15 am 
Beginner
Beginner

Joined: Thu Mar 01, 2007 9:40 am
Posts: 23
Location: UK
Could you post the hibernate code you're using to query?

_________________
Sam.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 20, 2007 6:35 am 
Newbie

Joined: Wed Mar 14, 2007 4:35 am
Posts: 7
This is the code

public java.util.List fetch(Object filter) throws DAOException {
List result = null;
if (filter==null){
throw new DAOException("Filter es null");
}else {
check(filter);
Session session = getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
result = session.createCriteria(filter.getClass().getName()).
add(Example.create(filter).ignoreCase().
enableLike(MatchMode.ANYWHERE)).list();
tx.commit();
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 20, 2007 6:43 am 
Beginner
Beginner

Joined: Thu Mar 01, 2007 9:40 am
Posts: 23
Location: UK
Probably you need to verify with straightforward calls first to ensure that your mapping file is correct:
Code:
public Group fetch(String id) throws DAOException {
  Session session = getSessionFactory().getCurrentSession();
  Transaction tx = session.beginTransaction();
  Group group = (Group)session.get(Group.class, id);
  tx.commit();
  session.close();
}

_________________
Sam.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 20, 2007 10:45 am 
Newbie

Joined: Wed Mar 14, 2007 4:35 am
Posts: 7
it seems it's working better. But I have another question. I have a table with these parameters (id,id_father) and these datas

GRO_ID GRO_ID_FATHER
72 NULL
73 72
74 72
75 72

with this change, my "where sentence" is where group0_.gro_id=72 but i want it would be where group0_.gro_id_father=72

This is my new code

Session session = getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
ESElement var = (ESElement) filter;
session.get(var.getClass(),var.getId());
result = session.createCriteria(filter.getClass().getName()).
add(Example.create(filter).ignoreCase().
enableLike(MatchMode.ANYWHERE)).list();
tx.commit();

where "filter" is the father

Thanks a lot for your help


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