-->
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.  [ 2 posts ] 
Author Message
 Post subject: [Mapping] Map avec objet et objet simple.
PostPosted: Thu Oct 23, 2008 8:32 am 
Beginner
Beginner

Joined: Fri May 23, 2008 4:37 am
Posts: 25
Hibernate version: 3.2.5

Mapping documents:
Code:
<class name="be.security.Resource" table="resources">
  <id name="url"><generator class="assigned" /></id>
      
  <property name="description" />
  <property name="system"/>
  <set name="roles" table="resources_roles" lazy="false">
    <key column="resUrl" />
    <many-to-many class="be.commons.beans.Role" column="roleName" lazy="false" />
  </set>   
</class>

<class name="be.commons.beans.Role" table="roles">
  <id name="name"><generator class="assigned" /></id>
   
  <property name="description"/>
  <property name="enabled" type="java.lang.Boolean"/>
  <property name="system" type="java.lang.Boolean" />
      
  <set name="users" table="users_roles" lazy="false" cascade="all">
    <key column="role" />
    <many-to-many column="uid" class="be.commons.beans.User" />
  </set>
</class>   

<class name="be.commons.Authorization" table="???">
  ???
</class>


Code between sessionFactory.openSession() and session.close(): Sans importance

Full stack trace of any exception that occurs: Pas d'exception

Name and version of the database you are using: PotgreSQL 8.2

The generated SQL (show_sql=true): Pas de SQL

Debug level Hibernate log excerpt: Pas de LOG
------------------------------------------------------------------------------------

Bonjour,

Je suis face à un problème dont je ne parviens pas à me sortir.
Je tente de mettre en place un système de droits d'accès avec les classes suivantes :
"Resource" : une ressource à "protéger", qui est en réalité une URL
"Role" : Un groupe d'utilisateurs
"User" : Un utilisateur
"Authorization" : Indique si on à l'accès en lecture, écriture et exécution (canRead(), canWrite(), canExeceute()).

Comme l'indiquent mes mappings (voir début du post) j'ai mappé les relations [Resource]1--n[Role]n--n[User]

Mais je dois également prendre en compte les authorisations, je voulais donc mettre en place une Map<Role, Autorization> dans le bean "Resource" à la place du Set actuel mais mon autorisation est représentée uniquement par un entier (linux like) et n'a pas d'autres propriétés.

Comment donc pourrais-je mapper la Map<Role, Autorization> alors qu'il n'y à pas de table ni d'identifiant pour le bean Autorization ?

Merci

Edit : Peut-être un UserType mais je ne suis pas sur que ça passe correctement dans la Map. Si ça passe correctement, avez vous un exemple de mapping ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 24, 2008 3:56 am 
Beginner
Beginner

Joined: Fri May 23, 2008 4:37 am
Posts: 25
Bonjour,

J'ai en partie solutionné mon problème en utilisant un UserType et le mapping suivant :
Code:
  <map name="authorizations" table="resources_roles" lazy="false">
    <key column="resUrl" />
    <index-many-to-many column="role" class="be.gervaisb.commons.beans.Role"/>
    <element column="auth" type="be.gervaisb.sics.dao.impl.spring.hibernate.usertypes.AuthorizationUserType" />
  </map>


L'ensemble fonctionne en sauvegarde et récupération mais me lance une exception losrque je tente un update.

Mon code est le suivant :
Code:
// Récupère un rôle existant an base de donnée
RoleDao roledao = daoFactory.getRoleDao();
Role tst = roledao.get("Test");
      
// Récupère une ressource qui possède déja le rôle "Test") [je l'ai sauvé avant]
ResourceDao dao = daoFactory.getResourcesDao();
Resource res = dao.getFromUrl("*.js");
System.out.println(res);

// Remplace l'autorisation pour le rôle   
res.getAuthorizations().put(tst, new Authorization(7));
dao.update(res); // Source de l'exception


Trace complète et logs utiles :
Quote:
2008-10-24 09:45:31,578 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 0, SQLState: null
2008-10-24 09:45:31,578 ERROR [org.hibernate.util.JDBCExceptionReporter] - L'élément du batch 0 /* insert collection row be.security.Resource.authorizations */ insert into resources_roles (resUrl, role, auth) values (*.js, Test, 7) a été annulé. Appeler getNextException pour en connaître la cause.
2008-10-24 09:45:31,578 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 0, SQLState: 23505
2008-10-24 09:45:31,578 ERROR [org.hibernate.util.JDBCExceptionReporter] - ERROR: duplicate key violates unique constraint "resources_roles_pkey1"
2008-10-24 09:45:31,578 ERROR [org.hibernate.event.def.AbstractFlushingEventListener] - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:170)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:420)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.update(HibernateTemplate.java:712)
at org.springframework.orm.hibernate3.HibernateTemplate.update(HibernateTemplate.java:708)
at be.dao.impl.spring.hibernate.GenericHibernateDao.update(GenericHibernateDao.java:71)
at be.dao.impl.spring.Test.main(Test.java:40)
Caused by: java.sql.BatchUpdateException: L'élément du batch 0 /* insert collection row be.security.Resource.authorizations */ insert into resources_roles (resUrl, role, auth) values (*.js, Test, 7) a été annulé. Appeler getNextException pour en connaître la cause.
at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2530)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1317)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:350)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2592)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)


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