-->
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: composite id with 2 foreign keys
PostPosted: Sat Jul 21, 2007 1:01 pm 
Newbie

Joined: Sat Jul 21, 2007 12:44 pm
Posts: 1
Hi everybody, i'm with some problems using composite id.

I have 3 tables:

City
--
idcity (PK)
name
--

Neighborhood
--
idcity (PK) (FK)
idneighbor (PK) (FK)
distance
--

Conditions
--
idconditions (PK)
idneighbor (FK)
status
--



My problem is that idcity and idneighbor are composite ids and foreign keys, in the same time, they're associated with idcity which is an PK in idcity.


My classes are:

public class Cidade {
private Integer idcidade;
private String nome;
private Set vizinhos;

//constructors and getters, setters
...
}


public class Vizinhanca {
private Integer id;
private Cidade cidade;
private Cidade vizinho;
private double distancia;

//constructors and getters, setters
...
}

public class Condicoes {
private Integer idcondicoes;
private Integer idvizinho;
private String status;

//constructors and getters, setters
...
}


public class CidadeCompositeId implements Serializable{

private Integer idcidade;
private Integer idvizinho;

public CidadeCompositeId() {
super();
}

public CidadeCompositeId(Integer idcidade, Integer idvizinho) {
super();
this.idcidade = idcidade;
this.idvizinho = idvizinho;
}

public Integer getIdcidade() {
return idcidade;
}

public void setIdcidade(Integer idcidade) {
this.idcidade = idcidade;
}

public Integer getIdvizinho() {
return idvizinho;
}

public void setIdvizinho(Integer idvizinho) {
this.idvizinho = idvizinho;
}

@Override
public int hashCode() {
//hashcode implementation
}

@Override
public boolean equals(Object obj) {
//equals implementation

}



My xml:

<hibernate-mapping package="com.maparot.model">

<class name="Cidade">

<id name="idcidade">
<generator class="increment"/>
</id>

<property name="nome"/>

<set name="vizinhos">
<key column="idcidade"/>
<one-to-many class="Vizinhanca"/>
</set>

</class>

</hibernate-mapping>



<class name="Vizinhanca">

<composite-id name="id" class="CidadeCompositeId">
<key-property name="idcidade"/>
<key-property name="idvizinho"/>
</composite-id>

<property name="distancia"/>

<many-to-one
name="cidade"
class="Cidade"
column="idcidade" insert="false" update="false"/>

<many-to-one
name="vizinho"
class="Cidade"
column="idcidade" insert="false" update="false"/>

</class>



And i got this error:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select max(idcity) from City
Exception in thread "main" org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.maparot.model.Neighborhood


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.