-->
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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate 3.1.1 prefixing unnecessary join columns
PostPosted: Wed Jan 25, 2006 8:43 am 
Beginner
Beginner

Joined: Tue Dec 21, 2004 11:53 am
Posts: 42
Hibernate version: 3.1.1

Mapping documents: Hibernate Annotations beta 8

Code between sessionFactory.openSession() and session.close(): Using Spring

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

The generated SQL (show_sql=true):

Hello there! I've really tried everthing here, I've posted at the EJB3 forum, and got no reply, please help me here!

I have entities that are mapped to views on oracle. Both of them have composite keys. I'm having problems when creating a OneToMany relationship between them. Here's some snippet of my code:

Code:
@Entity
@Table(name="VW_USUARIO")
@SuppressWarnings("serial")
public class Usuarioimplements BaseEntity {
   private UsuarioPK id;
   private String nome;
   private Cidade cidade;
   private Set<Produto> produtos;
   
   @Id
   public UsuarioPK getId() {
      return id;
   }
   @Column(name="NOME")
   public String getNome() {
      return nome;
   }
   @ManyToOne(cascade=CascadeType.REFRESH)
   @JoinColumn(name="CODCID")
   public Cidade getCidade() {
      return cidade;
   }
   @OneToMany(fetch=FetchType.EAGER,targetEntity=Produto.class,mappedBy="usuario")
@JoinColumns({@JoinColumn(name="CODCID"),@JoinColumn(name="CONTRA")})
   public Set getProdutos() {
      return produtos;
   }
}

@Embeddable
public class UsuarioPK implements Serializable { ...} //it implements equals and hashcode

@SuppressWarnings("serial")
@Entity
@Table(name="VW_PRODUTO")
public class Produto implements BaseEntity {
   private ProdutoPK id;
        private Usuario usuario;
        @Id
   public ProdutoPK getId() {
      return id;
   }
@ManyToOne
   public Usuario getUsuario() {
      return usuario;
   }


}


The problem here is that the SQL being generated adds a prefix to some of the join columns on the VW_PRODUTO I have the CODCID, CONTRA columns which are the FK from the Usuario View. Hibernate is prefixing this columns with the entity name:
Code:
select usuario0_.CONTRA as CONTRA3_3_, usuario0_.CODCID as CODCID3_3_, usuario0_.NOME as NOME3_3_, cidade1_.CODCID as CODCID2_0_, cidade1_.NOME as NOME2_0_, produtos2_.CODCID as CODCID5_, produtos2_.CONTRA as CONTRA5_, produtos2_.PONTO as PONTO5_, produtos2_.CONTRA as CONTRA4_1_, produtos2_.CODCID as CODCID4_1_, produtos2_.PONTO as PONTO4_1_, produtos2_.CODPROG as CODPROG4_1_, produtos2_.VALOR as VALOR4_1_, produtos2_.DTCONE as DTCONE4_1_, produtos2_.DTDESC as DTDESC4_1_, produtos2_.DESCRICAO as DESCRICAO4_1_, produtos2_.ID_STATUS as ID9_4_1_, produtos2_.LOCAL as LOCAL4_1_, produtos2_.usuario_CONTRA as usuario12_4_1_, produtos2_.usuario_CODCID as usuario13_4_1_, produtos2_.STATUS as STATUS4_1_, usuario3_.CONTRA as CONTRA3_2_, usuario3_.CODCID as CODCID3_2_, usuario3_.NOME as NOME3_2_ from VW_usuario usuario0_ left outer join TVA0600 cidade1_ on usuario0_.CODCID=cidade1_.CODCID left outer join VW_PONTO_PRODUTO produtos2_ on usuario0_.CONTRA=produtos2_.CODCID and usuario0_.CODCID=produtos2_.CONTRA left outer join VW_usuario usuario3_ on produtos2_.usuario_CONTRA=usuario3_.CONTRA and produtos2_.usuario_CODCID=usuario3_.CODCID where usuario0_.CONTRA=? and usuario0_.CODCID=?


As shown above produtos2_.CODCID as CODCID5_ is being selected but also produtos2_.usuario_CODCID

Could someone please help me here, If any other info is need I can provide at any time.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 8:49 am 
Beginner
Beginner

Joined: Tue Dec 21, 2004 11:53 am
Posts: 42
I've just noticed this line:

left outer join VW_PONTO_PRODUTO produtos2_ on assinante0_.CONTRA=produtos2_.CODCID and assinante0_.CODCID=produtos2_.CONTRA
left outer join VW_ASSINANTE assinante3_ on produtos2_.assinante_CONTRA=assinante3_.CONTRA and produtos2_.assinante_CODCID=assinante3_.CODCID



Well two joins? Is this because it's bidirectional (I'd like it to be unidirectional but I can;t get it to work either)

also, on the first join it's mixing the columns:
assinante0_.CONTRA=produtos2_.CODCID and assinante0_.CODCID=produtos2_.CONTRA

My PK are in the same order (I recall this from hibernate 2.1 and composite keys on OneToMany Relations where the order of the columns did matter, is this the same situation?)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 4:24 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
put the @§JoinColumn on the onwer side (ie the @ManyToOne side) not the @OneToMany

_________________
Emmanuel


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