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
|