-->
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.  [ 6 posts ] 
Author Message
 Post subject: Why so much selects ?! What is better !?
PostPosted: Fri Jan 28, 2005 2:58 pm 
Beginner
Beginner

Joined: Thu Dec 23, 2004 8:47 am
Posts: 32
Location: Brazil State:Rio Grande do Sul City:Porto Alegre
Hi all!

In a relationship one to many i just add one child to parent

like

Phonecall (child) to City(parent)
Phonecall (child) to People(parent)
Phonecall (child) to Employee(parent)

and the Parent already exists, so it does SIX (6) selects
before to insert in Phonecall,even i use lock.

How could i tune this queries?!

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 28, 2005 3:58 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
How could i tune this queries?!


which queries? which mapping file?
http://www.hibernate.org/ForumMailingli ... AskForHelp

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: 3 detached parents and 1 new child
PostPosted: Mon Jan 31, 2005 8:04 am 
Beginner
Beginner

Joined: Thu Dec 23, 2004 8:47 am
Posts: 32
Location: Brazil State:Rio Grande do Sul City:Porto Alegre
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 2.1.7

Mapping documents:

Child Mapping
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="hello.Ligacao" table="LIGACOES" lazy="true">
<id name="id" column="LIGACAO_ID" unsaved-value="any">
<generator class="increment" />
</id>
<property name="dataHora" column="DATAHORA" not-null="true"/>
<property name="motivo" column="MOTIVO" />
<property name="numero" column="NUMERO" not-null="true" />
<property name="duracao" column="DURACAO" not-null="true"/>

<many-to-one name="destino" column="DESTINO_ID" not-null="true"
class="hello.Destino"
update="false"
cascade="save-update"/>

<many-to-one name="pessoa" column="PESSOA_ID" not-null="true"
class="hello.Pessoa" cascade="save-update"/>

<many-to-one name="funcionario" column="FUNCIONARIO_ID" not-null="true"
class="hello.Funcionario" cascade="save-update"/>
</class>
</hibernate-mapping>


Parents Mapping

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="hello.Destino" lazy="true" table="DESTINOS">
<id name="id" column="DESTINO_ID" >
<generator class="increment"/>
</id>
<property name="nome" column="NOME" not-null="true" />
<property name="endereco" column="ENDERECO" not-null="true"/>
<property name="pais" column="PAIS" not-null="true"/>
<property name="telefoneCentral" column="TELEFONECENTRAL" not-null="true"/>
<set name="pessoasDestinadas"
cascade="all"
lazy="true"
inverse="true"

>
<key column ="DESTINO_ID"/>
<one-to-many class="hello.PessoaDestinada"/>
</set>
<set name="ramais" lazy="true" inverse="true" cascade="all" >
<key column="DESTINO_ID"/>
<one-to-many class="hello.Ramal"/>
</set>
<set name="ligacoes" lazy="true" inverse="true" cascade="all">
<key column="DESTINO_ID"/>
<one-to-many class="hello.Ligacao"/>
</set>
</class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="hello.Funcionario" table="FUNCIONARIOS" >
<id name="id" column="FUNCIONARIO_ID" unsaved-value="0">
<generator class="increment" />
</id>
<property name="matricula" column="MATRICULA" not-null="true" />
<property name="cargo" column="CARGO" not-null="true"/>
<property name="nome" column="NOME" not-null="true"/>
<property name="telefone" column="TELEFONE" not-null="true"/>
<property name="email" column="EMAIL" not-null="true"/>
<set name="ligacoes" lazy="true" inverse="true" cascade="all">
<key column="FUNCIONARIO_ID"/>
<one-to-many class="hello.Ligacao"/>
</set>
</class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="hello.Pessoa" table="PESSOAS" >
<id name="id" column="PESSOA_ID" >
<generator class="increment" />
</id>
<property name="nome" column="NOME" />
<property name="telefone" column="TELEFONE" />
<property name="celular" column="CELULAR" />
<set name="pessoasDestinadas"
lazy="true"
cascade="all"
inverse="true">
<key column ="PESSOA_ID"/>
<one-to-many class="hello.PessoaDestinada"/>
</set>

<set name="ligacoes" lazy="true" inverse="true" cascade="all">
<key column="PESSOA_ID"/>
<one-to-many class="hello.Ligacao"/>
</set>
</class>
</hibernate-mapping>



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


Here is the point .

I get from an internet session the 3 full parent objects already persisted, so they are detached objects.
I build a new Ligacao (PhoneCall) and add the 3 parents

destino.addLigacao();
funcionario.addLigacao();
pessoa.addLigacao();

and i get
- one insert
insert into LIGACOES (DATAHORA, MOTIVO, NUMERO, DURACAO, DESTINO_ID, PESSOA_ID, FUNCIONARIO_ID, LIGACAO_ID) values (?, ?, ?, ?, ?, ?, ?, ?)

- 3 updates
update DESTINOS set NOME=?, ENDERECO=?, PAIS=?, TELEFONECENTRAL=? where DESTINO_ID=?

update PESSOAS set NOME=?, TELEFONE=?, CELULAR=? where PESSOA_ID=?

update FUNCIONARIOS set MATRICULA=?, CARGO=?, NOME=?, TELEFONE=?, EMAIL=? where FUNCIONARIO_ID=?

but i do not want to update that 3 parents.

So what could i do ?!

The other way i tried was ..

s.get(Destino.class,destino.getId());
s.get(Funcionario.class,funcionario.getId());
s.get(Pessoa.class,pessoa.getId());

destino.addLigacao();
pessoa.addLigacao();
funcionario.addLigacao();

to ensure that the objects still exist on database.
That does six selects !! and if i try to lock , Hibernate says that has an object that is dirty and do not complete the operation.


Hibernate: select destino0_.DESTINO_ID as DESTINO_ID0_, destino0_.NOME as NOME0_, destino0_.ENDERECO as ENDERECO0_, destino0_.PAIS as PAIS0_, destino0_.TELEFONECENTRAL as TELEFONE5_0_ from DESTINOS destino0_ where destino0_.DESTINO_ID=?
Hibernate: select pessoa0_.PESSOA_ID as PESSOA_ID0_, pessoa0_.NOME as NOME0_, pessoa0_.TELEFONE as TELEFONE0_, pessoa0_.CELULAR as CELULAR0_ from PESSOAS pessoa0_ where pessoa0_.PESSOA_ID=?
Hibernate: select funcionari0_.FUNCIONARIO_ID as FUNCIONA1_0_, funcionari0_.MATRICULA as MATRICULA0_, funcionari0_.CARGO as CARGO0_, funcionari0_.NOME as NOME0_, funcionari0_.TELEFONE as TELEFONE0_, funcionari0_.EMAIL as EMAIL0_ from FUNCIONARIOS funcionari0_ where funcionari0_.FUNCIONARIO_ID=?
Hibernate: select ligacoes0_.DESTINO_ID as DESTINO_ID__, ligacoes0_.LIGACAO_ID as LIGACAO_ID__, ligacoes0_.LIGACAO_ID as LIGACAO_ID2_, ligacoes0_.DATAHORA as DATAHORA2_, ligacoes0_.MOTIVO as MOTIVO2_, ligacoes0_.NUMERO as NUMERO2_, ligacoes0_.DURACAO as DURACAO2_, ligacoes0_.DESTINO_ID as DESTINO_ID2_, ligacoes0_.PESSOA_ID as PESSOA_ID2_, ligacoes0_.FUNCIONARIO_ID as FUNCIONA8_2_, pessoa1_.PESSOA_ID as PESSOA_ID0_, pessoa1_.NOME as NOME0_, pessoa1_.TELEFONE as TELEFONE0_, pessoa1_.CELULAR as CELULAR0_, funcionari2_.FUNCIONARIO_ID as FUNCIONA1_1_, funcionari2_.MATRICULA as MATRICULA1_, funcionari2_.CARGO as CARGO1_, funcionari2_.NOME as NOME1_, funcionari2_.TELEFONE as TELEFONE1_, funcionari2_.EMAIL as EMAIL1_ from LIGACOES ligacoes0_ left outer join PESSOAS pessoa1_ on ligacoes0_.PESSOA_ID=pessoa1_.PESSOA_ID left outer join FUNCIONARIOS funcionari2_ on ligacoes0_.FUNCIONARIO_ID=funcionari2_.FUNCIONARIO_ID where ligacoes0_.DESTINO_ID=?
Hibernate: select ligacoes0_.PESSOA_ID as PESSOA_ID__, ligacoes0_.LIGACAO_ID as LIGACAO_ID__, ligacoes0_.LIGACAO_ID as LIGACAO_ID1_, ligacoes0_.DATAHORA as DATAHORA1_, ligacoes0_.MOTIVO as MOTIVO1_, ligacoes0_.NUMERO as NUMERO1_, ligacoes0_.DURACAO as DURACAO1_, ligacoes0_.DESTINO_ID as DESTINO_ID1_, ligacoes0_.PESSOA_ID as PESSOA_ID1_, ligacoes0_.FUNCIONARIO_ID as FUNCIONA8_1_, funcionari1_.FUNCIONARIO_ID as FUNCIONA1_0_, funcionari1_.MATRICULA as MATRICULA0_, funcionari1_.CARGO as CARGO0_, funcionari1_.NOME as NOME0_, funcionari1_.TELEFONE as TELEFONE0_, funcionari1_.EMAIL as EMAIL0_ from LIGACOES ligacoes0_ left outer join FUNCIONARIOS funcionari1_ on ligacoes0_.FUNCIONARIO_ID=funcionari1_.FUNCIONARIO_ID where ligacoes0_.PESSOA_ID=?
Hibernate: select ligacoes0_.FUNCIONARIO_ID as FUNCIONA8___, ligacoes0_.LIGACAO_ID as LIGACAO_ID__, ligacoes0_.LIGACAO_ID as LIGACAO_ID1_, ligacoes0_.DATAHORA as DATAHORA1_, ligacoes0_.MOTIVO as MOTIVO1_, ligacoes0_.NUMERO as NUMERO1_, ligacoes0_.DURACAO as DURACAO1_, ligacoes0_.DESTINO_ID as DESTINO_ID1_, ligacoes0_.PESSOA_ID as PESSOA_ID1_, ligacoes0_.FUNCIONARIO_ID as FUNCIONA8_1_, pessoa1_.PESSOA_ID as PESSOA_ID0_, pessoa1_.NOME as NOME0_, pessoa1_.TELEFONE as TELEFONE0_, pessoa1_.CELULAR as CELULAR0_ from LIGACOES ligacoes0_ left outer join PESSOAS pessoa1_ on ligacoes0_.PESSOA_ID=pessoa1_.PESSOA_ID where ligacoes0_.FUNCIONARIO_ID=?
Hibernate: insert into LIGACOES (DATAHORA, MOTIVO, NUMERO, DURACAO, DESTINO_ID, PESSOA_ID, FUNCIONARIO_ID, LIGACAO_ID) values (?, ?, ?, ?, ?, ?, ?, ?)




Thanks in advance ! and sorry by my english :)


Full stack trace of any exception that occurs:

Name and version of the database you are using:Sybase

The generated SQL (show_sql=true):


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 31, 2005 8:32 am 
Beginner
Beginner

Joined: Thu Dec 23, 2004 8:47 am
Posts: 32
Location: Brazil State:Rio Grande do Sul City:Porto Alegre
I think it happens because addLigacao method
and i call it 3 times for each one parent


public void addLigacao(Ligacao ligacao) {
ligacao.setParent(this);
this.ligacoes.add(ligacao);


}


but i found another way with just 3 selects and 1 insert, saving "manually" the child ligacao.


dest=(Destino) s.get(Destino.class,destino.getId());
pess= (Pessoa) s.get(Pessoa.class,pessoa.getId());
func=(Funcionario)s.get(Funcionario.class,funcionario.getId());


ligacao.setDestino(dest);
ligacao.setFuncionario(func);
ligacao.setPessoa(pess);

s.save(ligacao);


some help!?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 31, 2005 11:46 am 
Beginner
Beginner

Joined: Thu Dec 23, 2004 8:47 am
Posts: 32
Location: Brazil State:Rio Grande do Sul City:Porto Alegre
Am i missing something to be answered?

thanks


Top
 Profile  
 
 Post subject: Simplifying the question
PostPosted: Mon Jan 31, 2005 12:39 pm 
Beginner
Beginner

Joined: Thu Dec 23, 2004 8:47 am
Posts: 32
Location: Brazil State:Rio Grande do Sul City:Porto Alegre
Simplifying the question

Scenario :
I get the parent from an internet session , and i pass it to a session to persist with a new child.
There is a way to add a new child to an existing parent without updating the parent after insert of a new new child !?
Or without selecting (session.get) the parents inside the hibernate session !?
If i lock the parent , hibernate says that an object is dirty and do not complete the operation.


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