Hello,
I'm new to hibernate, then I'm sorry If this is a non-sense, even knowing that it works in hibernate with XDoclets instead of annotations.
I'm trying to map a composite PK, but the PK's fields are associations.
If I declare the PK fields with Long type, and read the PK's codes, everything works fine. But If I try to load the table associated with this codes, I get a "could not deserialize" Exception.
The PK fields are associated with another tables because this is a ManyToMany relationship with properties inside it. So I mapped the relationship table, and created 2 OneToMany realationships in the other classes (actually only one OneToMany, because it isn't bidirectional).
I looked the forum's questions and manuals but didn't find this problem/example. Is there a way to do that? If there isn't, is there a better way to do this (ternary association right?) ?
Thanks in advance.
Leonardo Barbosa
Quote:
The tables
PB_PERFIL
codpfl
nompfl
PB_PERMISSAO
codprm
codtipprm
PB_PERFIL_PERMISSAO (relationship table)
codpfl
codprm
tipace (the property of the relationship)
Quote:
The mapping classes:
Code:
// Relationship table
@Entity
@Table(name = "PB_PERFIL_PERMISSAO")
public class PerfilPermissao implements java.io.Serializable {
// Fields
@Id
private PerfilPermissaoId id;
// Constructors
public PerfilPermissao() {
}
public PerfilPermissaoId getId() {
return id;
}
public void setId(PerfilPermissaoId id) {
this.id = id;
}
}
// Relationship table PK
@Embeddable
public class PerfilPermissaoId implements java.io.Serializable {
// Fields
private Perfil codpfl;
private Permissao codprm;
private String tipace;
// Constructors
/** full constructor */
public PerfilPermissaoId(Perfil codpfl, Permissao codprm, String tipace) {
this.codpfl = codpfl;
this.codprm = codprm;
this.tipace = tipace;
}
// Property accessors
@ManyToOne(targetEntity=Perfil.class)
@JoinColumn(name="codpfl")
public Perfil getCodpfl() {
return codpfl;
}
public void setCodpfl(Perfil codpfl) {
this.codpfl = codpfl;
}
@ManyToOne(targetEntity=Permissao.class)
@JoinColumn(name="codprm")
public Permissao getCodprm() {
return codprm;
}
public void setCodprm(Permissao codprm) {
this.codprm = codprm;
}
public String getTipace() {
return tipace;
}
public void setTipace(String tipace) {
this.tipace = tipace;
}
public boolean equals(Object other) {
L.debug("Equals ");
if ((this == other)) return true;
if ((other == null)) return false;
if (!(other instanceof PerfilPermissaoId)) return false;
PerfilPermissaoId castOther = (PerfilPermissaoId) other;
return ((this.getCodpfl().equals(castOther.getCodpfl())) &&
(this.getCodprm().equals(castOther.getCodprm())) && this.getTipace().trim().equals(castOther.getTipace().trim()));
}
public int hashCode() {
int result = 17;
result = 37 * result + (getCodpfl() == null ? 0 : this.getCodpfl().hashCode());
result = 37 * result + (getCodprm() == null ? 0 : this.getCodprm().hashCode());
result = 37 * result + (getTipace() == null ? 0 : this.getTipace().hashCode());
return result;
}
}
The two tables mapping:
@Entity
@Table(name = "PB_PERFIL")
public class Perfil implements java.io.Serializable {
private Long codpfl;
private List<PerfilPermissao> permissoes;
....
// Property accessors
@Id
public Long getCodpfl() {
return this.codpfl;
}
public void setCodpfl(Long codpfl) {
this.codpfl = codpfl;
}
@OneToMany(targetEntity=PerfilPermissao.class)
@JoinColumn(name="codpfl")
public List<PerfilPermissao> getPermissoes() {
return permissoes;
}
public void setPermissoes(List<PerfilPermissao> permissoes) {
this.permissoes = permissoes;
}
}
@Entity
@Table(name = "PB_PERMISSAO")
public class Permissao implements java.io.Serializable {
private Long codprm;
@Id
public Long getCodprm() {
return this.codprm;
}
public void setCodprm(Long codprm) {
this.codprm = codprm;
}
....
}
Hibernate version: 3.1 with annotations beta 8
Mapping documents:<session-factory>
<property name="connection.username">XXXXXX</property>
<property name="connection.url">XXXXXX</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="myeclipse.connection.profile">XXXX</property>
<property name="connection.password">XXXXX</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<mapping class="br.com.brti.si.sistemas.hibernate.Empresa" />
<mapping class="br.com.brti.si.sistemas.hibernate.Site" />
<mapping class="br.com.brti.si.sistemas.hibernate.Usuario" />
<mapping class="br.com.brti.si.sistemas.hibernate.Perfil" />
<mapping class="br.com.brti.si.sistemas.hibernate.TipoPermissao" />
<mapping class="br.com.brti.si.sistemas.hibernate.Permissao" />
<mapping class="br.com.brti.si.sistemas.hibernate.PerfilPermissao" />
</session-factory>
Code between sessionFactory.openSession() and session.close():Code:
List perfis = null;
if (codpfl != null && !"".equals(codpfl)) perfis = hybSession.createQuery("from Perfil where codpfl = ?").setString(0, codpfl).list();
if (perfis != null){
for (Iterator i = perfis.iterator(); i.hasNext(); ){
out.println("Perfil 1:<BR>");
Perfil perfil = (Perfil) i.next();
out.printf("%s (%s)<br>", perfil.getNompfl(), perfil.getCodpfl());
List<PerfilPermissao> permissoes = perfil.getPermissoes();
for(PerfilPermissao permissao : permissoes){
out.printf("%s : %s<BR>", permissao.getId().getCodprm(), permissao.getId().getTipace());
}
}
}else{
out.println("Nao tem perfis!");
}
Full stack trace of any exception that occurs:
org.hibernate.type.SerializationException: could not deserialize
at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:211)
at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:234)
at org.hibernate.type.SerializableType.fromBytes(SerializableType.java:78)
at org.hibernate.type.SerializableType.get(SerializableType.java:39)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:113)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:102)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)
at org.hibernate.type.ComponentType.hydrate(ComponentType.java:506)
at org.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:229)
at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1088)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:554)
at org.hibernate.loader.Loader.doQuery(Loader.java:689)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1919)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:71)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:520)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1695)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:246)
at br.com.brti.si.sistemas.publicador.servlets.TestePermissao.service(TestePermissao.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:667)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Caused by: java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:753)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:268)
at org.hibernate.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:246)
at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:203)
... 36 more
Name and version of the database you are using:
Oracle 9
The generated SQL (show_sql=true):
Hibernate: select perfil0_.codpfl as codpfl3_, perfil0_.codsit as codsit3_, perfil0_.staatv as staatv3_, perfil0_.dtains as dtains3_, perfil0_.nompfl as nompfl3_ from PB_PERFIL perfil0_ where perfil0_.codpfl=?
Hibernate: select site0_.codsit as codsit1_1_, site0_.codemp as codemp1_1_, site0_.nomsit as nomsit1_1_, site0_.staatv as staatv1_1_, empresa1_.codemp as codemp0_0_, empresa1_.nomemp as nomemp0_0_ from PB_SITE site0_ left outer join PB_EMPRESA empresa1_ on site0_.codemp=empresa1_.codemp where site0_.codsit=?
Hibernate: select permissoes0_.codpfl as codpfl1_, permissoes0_.codprm as codprm1_, permissoes0_.tipace as tipace1_, permissoes0_.codpfl as codpfl6_0_, permissoes0_.codprm as codprm6_0_, permissoes0_.tipace as tipace6_0_ from PB_PERFIL_PERMISSAO permissoes0_ where permissoes0_.codpfl=?
[/i]