-->
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: many-to-many problem
PostPosted: Tue Oct 24, 2006 6:14 pm 
Newbie

Joined: Tue Oct 24, 2006 5:50 pm
Posts: 3
I have a problem with a bidirectional many-to-many asociation, the problem is this:

I have two java beans, Student and Profesor, with their getters and setters respectly, that relation has to generate a new table that include the two identifier:


public class Estudiante
{
private String nombre;
private Integer solapin_e;
private Set<Profesor> profesores = new HashSet<Profesor>();
.....

}

public class Profesor
{
private String nombre;
private Integer solapin_p;
private Set<Estudiante> estudiantes = new HashSet<Estudiante>();
....
}

When i want some kind of operation(delete, save-update,etc), in the principals tables(Estudiante, Profesor), the actions must be realized in cascade to the link table only.
If i want delete an Estudiante, the aplication must be delete the Estudiante and all the instances of him in the link table only, this changes no have any reason to be realized in the table Profesor.

I have serious problem with generate a Data Base that expires with these requirements using Hibernate, I prove the automappings options that Exadel provides and nothing,
PLEASE, I APPRECIATE ANY KIND OF HELP

pd: Sorry with the English, I'm not speak in that language nothing well.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 24, 2006 11:52 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Do you have the cascade attribute setup on both the mappings? Having a cascade="delete" should delete the entries from the associated join table when the original model is deleted.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 2:49 am 
Newbie

Joined: Tue Oct 24, 2006 5:50 pm
Posts: 3
I try everything that i know, the cascade attribute setup on both the mappings with the value of delete, and nothing, this is the error that its throws..

ERROR: update or delete on "estudiante" violates foreign key constraint on estudiante_profesor
DETAIL key(solapin_e)=(12454) is still referenced from table estudiante_profesor

so, when i check de source code for the generated data base, and more especificaly for the link table i found this:

CREATE TABLE "public"."estudiante_profesor" (
"solapin_p" INTEGER NOT NULL,
"solapin_e" INTEGER NOT NULL,
CONSTRAINT "estudiante_profesor_pkey" PRIMARY KEY("solapin_e", "solapin_p"),
CONSTRAINT "solapin_e" FOREIGN KEY ("solapin_e")
REFERENCES "public"."profesor"("solapin_p")
ON DELETE NO ACTION
ON UPDATE NO ACTION
NOT DEFERRABLE,
CONSTRAINT "solapin_p" FOREIGN KEY ("solapin_p")
REFERENCES "public"."estudiante"("solapin_e")
ON DELETE NO ACTION
ON UPDATE NO ACTION
NOT DEFERRABLE
) WITHOUT OIDS;

like you can see, the actions on delete, on update are disabled, and i don't have any idea about it.

Making a summarize, hibernate is not generate te correct sentences of SQL that corresponding with my mappings, i don't know what, check the mappings files:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

this is the mapping for the bean Estudiante
<hibernate-mapping schema="public" package="beans">
<class name="Estudiante" table="estudiante" schema="public" optimistic-lock="none">
<id name="solapin_e" type="integer" unsaved-value="null" column="solapin_e">
<generator class="increment"/>
</id>
<property name="nombre" type="string" column="nombre"/>

<set name="profesores" table="estudiante_profesor" inverse="true" cascade="delete">
<key foreign-key="solapin_p" column="solapin_p"/>
<many-to-many entity-name="beans.Profesor" foreign-key="solapin_e" column="solapin_e"/>
</set>
</class>
</hibernate-mapping>



this is the mapping for the bean Profesor

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping schema="public" package="beans">
<class name="Profesor" table="profesor" schema="public" optimistic-lock="none">
<id name="solapin_p" type="integer" unsaved-value="null" column="solapin_p">
<generator class="increment"/>
</id>
<property name="nombre" type="string" column="nombre"/>

<set name="estudiantes" table="estudiante_profesor" cascade="delete">
<key foreign-key="solapin_e" column="solapin_e"/>
<many-to-many entity-name="beans.Estudiante" foreign-key="solapin_p" column="solapin_p"/>
</set>
</class>
</hibernate-mapping>

and this is the hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/pp1</property>
<property name="hibernate.connection.username">admin</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property name="hibernate.order_updates">true</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="beans/Estudiante.hbm.xml"/>
<mapping resource="beans/Profesor.hbm.xml"/>
</session-factory>
</hibernate-configuration>



WHAT DO YOU THINK???????


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.