-->
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.  [ 4 posts ] 
Author Message
 Post subject: Error: hibernate using Bag semantic with Set ?
PostPosted: Fri Mar 10, 2006 7:57 pm 
Newbie

Joined: Fri Mar 03, 2006 3:47 pm
Posts: 6
Hello,

I'm trying to avoid bag semantic with the property perfis in this Entity.

According to the documentation, I should change the type from List to Set :

Code:
@Entity
@Table(name = "PB_USUARIO")
@SequenceGenerator(name="SEQ_USUARIO", sequenceName="PB_USUARIO_SEQ")
public class Usuario  implements java.io.Serializable {

   private Set<Perfil> perfis;
...

    @ManyToMany
    @JoinTable(
        name="PB_USUARIO_PERFIL"
        ,joinColumns={@JoinColumn(name="codusr")}
        ,inverseJoinColumns={@JoinColumn(name="codpfl")}
    )
    public Set<Perfil> getPerfis() {
        return perfis;
    }
    public void setPerfis(Set<Perfil> perfis) {
       this.perfis = perfis;
    }

}


But hibernate still deletes my records inside PB_USUARIO_PERFIL :

Hibernate: delete from PB_USUARIO_PERFIL where codusr=?
Hibernate: insert into PB_USUARIO_PERFIL (codusr, codpfl) values (?, ?)
Hibernate: insert into PB_USUARIO_PERFIL (codusr, codpfl) values (?, ?)
Hibernate: insert into PB_USUARIO_PERFIL (codusr, codpfl) values (?, ?)


Then I tried to change it to List again, but using de @IndexColumn + @OrderBy

Code:
@Entity
@Table(name = "PB_USUARIO")
@SequenceGenerator(name="SEQ_USUARIO", sequenceName="PB_USUARIO_SEQ")
public class Usuario  implements java.io.Serializable {

    private Set<Perfil> perfis;
...

    @ManyToMany
    @JoinTable(
        name="PB_USUARIO_PERFIL"
        ,joinColumns={@JoinColumn(name="codusr")}
        ,inverseJoinColumns={@JoinColumn(name="codpfl")}
    )
    @IndexColumn(name="codusr")
    @OrderBy("codusr")
    public List<Perfil> getPerfis() {
        return perfis;
    }
    public void setPerfis(List<Perfil> perfis) {
       this.perfis = perfis;
    }

}


But the SessionFactory thrown an exception when I tried to run configure()

Code:
           sessionFactory = new AnnotationConfiguration().configure("br/com/brti/si/sistemas/hibernate/hibernate.cfg.xml").buildSessionFactory();


I'm using
<mapping class="XXX">
for mapping all the Entitys, wo any special property.


[10/03/2006 20:22:35] 10 - SEVERE - br.com.brti.si.L : Initial SessionFactory creation failed.org.hibernate.MappingException: Repeated column in mapping for collection: br.com.brti.si.sistemas.hibernate.Usuario.perfis column: codusr
[10/03/2006 20:22:35] 10 - SEVERE - br.com.brti.si.L : Erro na inicializaĆ§Ć£o do publicador : null
java.lang.ExceptionInInitializerError
at br.com.brti.si.sistemas.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:29)
at br.com.brti.si.sistemas.publicador.servlets.Inicializador.init(Inicializador.java:32)
at javax.servlet.GenericServlet.init(GenericServlet.java:211)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1105)



If I tried to remove the joinColumns and/or inverseJoinColumns properties from @JoinTable, to avoid "Repeated column in mapping ..." error, hibernate used the wrong columns to do the join (as I expected...)

What should I do to avoid bag sematic???

Thanks in advance,
Leonardo Barbosa

Hibernate version:
3.1 with annotations beta8


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 12, 2006 5:13 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you cannot mix @IndexColumn and @OrderBy, this does not make sense

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 9:40 am 
Newbie

Joined: Fri Mar 03, 2006 3:47 pm
Posts: 6
emmanuel wrote:
you cannot mix @IndexColumn and @OrderBy, this does not make sense


Thanks for your help.
I didn't quite understood the meaning of @IndexColumn. I used it because the documentation was clear:

So specifically, java.util.List collections wo @OrderBy nor @org.hibernate.annotations.IndexColumn are going to be considered as bags.
http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/#entity-mapping-association-collections

I tried them one at a time: @IndexColumn and @OrderBy, but didn't work too. @indexColumn is the index in the database that hibernate should use? I think I really didin't get this one. (I got @OrderBy, at least :)
I tried to use java.util.Set either, but guess what? Bag semantic again...
I just can't avoid Hibernate deleting my records and inserting everything again.

Am I right about that? Bag semantic will delete everything, and reinsert.
List, Set and Map semantics will insert and delete only the modified fields. Aint that right?

As I couldn't avoid delete, and I have a method that have access only to some items from the Bean List, and I needed to update the list, I created a workarround.

FYI, I created a method that: gets the list from the Bean, get a map from the servlet, remove the items from the Bean-list that are NULL in the servlet-map (key exists AND value are null), insert the items from the servlet-map that doesn't existis in the Bean-list, and let the items in the Bean-list, that doesn't existis in the Servlet-map's keys, unchanged.

It seams to work (after I changed the relationship in the Bean to FetchType.EAGER).

If anybody knows how tell hibernate to do this (or anything similar), please let me know.

Thanks,
Leonardo Barbosa


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 8:29 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I do not reproduce this behavior for Sets, there should be something wrong somewhere

_________________
Emmanuel


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