-->
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.  [ 1 post ] 
Author Message
 Post subject: Probleme jointure many to many delete...
PostPosted: Tue Jul 22, 2008 6:01 am 
Newbie

Joined: Tue Jul 22, 2008 5:05 am
Posts: 1
Location: France
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:2.1

Name and version of the database you are using:Oracle 10g

The generated SQL (show_sql=true):

Bonjour,

Je travail sur un model de classes métier qui permet la gestion d'utilisateurs auxquels on va leurs associer 0 - n rôles.

Lors d'un insert ou update d'utilisateur tout fonctionne correctement,
ce n'est qu'apres une suppression que l'application (spring-hibernate) ne va plus fonctionner,
en effet si j'essaie de faire un update d'utilisateur après suppression hibernate va essayer de mettre à jour un utilisateur qui n'existe déjà plus en base.

Erreur :

org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.ubp.odessa.auth.impl.User with id 161; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.ubp.odessa.auth.impl.User with id 161

l'User possédant l'Id 161 n'existe plus en base, je ne comprend pas, il n'y aurait pas un problème de rafraichissement...

Voici le configuration des classes métier :

Role.java :

@ManyToMany(
fetch =FetchType.EAGER,
mappedBy = "roles",
targetEntity = User.class,
cascade={CascadeType.REFRESH}
)

User.java :

@ManyToMany(
targetEntity=Role.class,
fetch=FetchType.EAGER,
cascade={CascadeType.MERGE,CascadeType.REFRESH,CascadeType.PERSIST}
)
@JoinTable(
name="tbUserRole",
joinColumns=@JoinColumn(name="fdUserId", referencedColumnName = "fdUserId"),
inverseJoinColumns=@JoinColumn(name="fdRoleId", referencedColumnName = "fdRoleId")
)


classe de mise à jour des données manuellement...

UserDAOImpl :

public class UserDAOImpl extends BaseDAO implements UserDAO {

@Override
@Transactional
public void save(DomainObject entity) {
try {
IUser user =(IUser)entity;

// Attach roles;
Set<Role> roles =new HashSet<Role>();
roles.addAll( user.getRoles() );

//user.getRoles().clear();

getJpaTemplate().persist(entity);

//getJpaTemplate().refresh(user);

for( Role role : roles ) {
role.getUsers().add( (User)user );
Role attachedRole =(Role)getJpaTemplate().merge(role);

//getJpaTemplate().refresh(role);

//getJpaTemplate().merge(role);

user.getRoles().add( attachedRole );
}

user.getRoles().clear();
getJpaTemplate().merge(entity);
getJpaTemplate().flush();

} catch (RuntimeException re) {
logger.error("save failed", re);

re.printStackTrace();
throw re;
}

}

@Override
@Transactional
public DomainObject update(DomainObject entity) {
IUser user =(IUser)entity;

try {
// Attach roles;
Set<Role> roles =new HashSet<Role>();
roles.addAll( user.getRoles() );

user.getRoles().clear();

getJpaTemplate().merge(entity);

for( Role role : roles ) {
role.getUsers().add( (User)user );
//Role attachedRole =(Role)getJpaTemplate().merge(role);

//getJpaTemplate().refresh(role);

//pas approprie lors de l ajout dun role
getJpaTemplate().merge(role);

user.getRoles().add( role );
}

getJpaTemplate().merge(entity);
getJpaTemplate().flush();

} catch (RuntimeException re) {
logger.error("update failed", re);
re.printStackTrace();
throw re;
}

return user;
}

@Override
@Transactional
public void delete( Integer entityPK ) throws Exception {
try {
//IUser user = (IUser)getJpaTemplate().getReference( Class.forName(getDomainObjectClassName()), entityPK);

IUser user = (IUser)getJpaTemplate().find( com.ubp.odessa.auth.impl.User.class , entityPK.intValue());

Set<Role> roles =new HashSet<Role>();
roles.addAll( user.getRoles() );

getJpaTemplate().merge(user);

for( Role role : roles ) {
role.getUsers().remove( user );

getJpaTemplate().merge( role );

//getJpaTemplate().refresh( role );
}

user.getRoles().clear();
getJpaTemplate().remove(user);
getJpaTemplate().flush();
//getJpaTemplate().refresh( user );


} catch (RuntimeException re) {
logger.error("delete failed", re);
re.printStackTrace();
throw re;
}
}
...

Tout en sachant que si on laisse hibernate JPA se débrouiller cela ne fonctionne pas :

BaseDAO :

@Override
@Transactional
public void save(DomainObject entity) {
try {
getJpaTemplate().persist(entity);

} catch (RuntimeException re) {
logger.error("save failed", re);
throw re;
}
}

@Transactional
public void delete(DomainObject entity) {
try {
DomainObject dob = (DomainObject)getJpaTemplate().getReference(entity.getClass(), entity.getId());

getJpaTemplate().remove(dob);
} catch (RuntimeException re) {
logger.error("delete failed", re);
throw re;
}
}

@Transactional
public void delete( Integer entityPK ) throws Exception {
try {
DomainObject dob = (DomainObject)getJpaTemplate().getReference( Class.forName(getDomainObjectClassName()), entityPK);

getJpaTemplate().remove(dob);
} catch (RuntimeException re) {
logger.error("delete failed", re);
throw re;
}
}

@Transactional
public DomainObject update(DomainObject entity) {
try {
DomainObject result = (DomainObject)getJpaTemplate().merge(entity);

return result;
} catch (RuntimeException re) {
logger.error("update failed", re);
throw re;
}
}
=> ne fonctionne pas...

Merci de me dire comment je pourrais faire pour que la synchro. entre les Users et Roles mais aussi entre (hibernate jpa) et oracle se fasse correctement.

_________________
Greg


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.