-->
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: How to map relationship as primary key
PostPosted: Thu Sep 21, 2006 7:04 pm 
Newbie

Joined: Tue Sep 19, 2006 7:07 pm
Posts: 9
Sorry for my bad english.

I've two classes: endereco and pessoa

There's a one-to-one relationship between both. The primary key of person is transfered to adress as foreign key and it's foreign key is the primary key to.

The DDL of tables are the follow:

CREATE TABLE pessoa (
CODIGO_PESS NUMBER(6,0) NOT NULL,
NOME VARCHAR2(40),
TELEFONE VARCHAR2(14),
IDADE NUMBER(3,0)
);

ALTER TABLE pessoa ADD CONSTRAINT pk_pess PRIMARY KEY ( CODIGO_PESS );

CREATE TABLE endereco (
CODIGO_PESS NUMBER(6,0) NOT NULL,
CODIGO_AREA NUMBER(6,0) NOT NULL,
RUA VARCHAR2(40),
NUMERO NUMBER(5,0),
BAIRRO VARCHAR2(25),
CIDADE VARCHAR2(25),
ESTADO VARCHAR2(2),
CEP VARCHAR2(9)
);

ALTER TABLE endereco ADD CONSTRAINT pk_ende PRIMARY KEY ( CODIGO_PESS );

ALTER TABLE endereco ADD CONSTRAINT fk_ende_pess01 FOREIGN KEY ( CODIGO_PESS ) REFERENCES pessoa ( CODIGO_PESS );

ALTER TABLE endereco ADD CONSTRAINT fk_ende_area02 FOREIGN KEY ( CODIGO_AREA ) REFERENCES area ( CODIGO_AREA );


My endereco.java class is the follow:

public class Endereco {

private Pessoa pessoa;
private Vendedor vendedor;
private Area area;
private String rua;
private int numero;
private String bairro;
private String cidade;
private String estado;
private String cep;

public Endereco() {
super();
}

public Pessoa getPessoa() {
return cliente;
}

public void setPessoa(Pessoa pessoa) {
this.pessoa = pessoa;
}

...
}

How should be the endereco.hbm.xml file once the id column is in another class (pessoa)?

Should I have both the atribute "int codigoPess" and "Pessoa pessoa" in the endereco.java class and map the id to codigoPess atribute of endereco and a relationship one-to-one to pessoa atribute or it will be a redundance information?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 2:29 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
use a one-to-one association with constrained="true" attribute.

see section 5.1.11. one-to-one of Hibernate documents.
--------------

don't forget credits.

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject: where is thw problem?
PostPosted: Fri Sep 22, 2006 12:15 pm 
Newbie

Joined: Tue Sep 19, 2006 7:07 pm
Posts: 9
My map class:

pessoa.hbm.xml
<class name="dto.Pessoa"
table="pessoa">

<id name="codigoPess" column="codigo_pess" type="java.lang.Integer" unsaved-value="0" >
<generator class="increment">
</generator>
</id>

<one-to-one name="endereco"
class="dto.Endereco"
column="codigo_pess"/>

</class>

endereco.hbm.xml
<class name="dto.Endereco"
table="endereco">

<id name="codigoPess" column="codigo_pess" type="java.lang.Integer" unsaved-value="0" >
<generator class="foreign">
<param name="property">pessoa</param>
</generator>
</id>

<one-to-one name="pessoa"
class="dto.Pessoa"
constrained="true"/>

</class>

The navigability between both endereco and pessoa is for two sides.
I'm receiving the error message:

Caused by: org.hibernate.MappingException: Could not read mappings from resource: dto/Cliente.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:484)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1443)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1411)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1392)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1368)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1288)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1274)
at dto.HibernateUtil.<clinit>(HibernateUtil.java:16)
... 33 more
Caused by: org.hibernate.MappingException: invalid mapping
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:424)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:481)
... 40 more
Caused by: org.xml.sax.SAXParseException: Attribute "column" must be declared for element type "one-to-one".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)...

is it because I made the one-to-one map of pessoa with the column making reference to his own atriubute?

if I disable the one-to-one map of pessoa and change the nivigability for endereco to pessoa only, I receive the follow error message:


Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for codigoPess in class dto.Endereco
at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:282)...

I really haven't a getter for codigoPess in endereco.java since this atribute is on the pessoa.java that is the related class in the id map property of endereco.

How can I map this?


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.