Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: use composite datatype with hibernate
PostPosted: Sat Oct 09, 2010 6:18 am 
Newbie

Joined: Sat Oct 09, 2010 1:58 am
Posts: 3
hi
i want to use composite type for example address in my project in netbean
can anybody help me????


Top
 Profile  
 
 Post subject: Re: use composite datatype with hibernate
PostPosted: Sun Oct 10, 2010 9:13 am 
Regular
Regular

Joined: Sun Feb 14, 2010 3:29 pm
Posts: 58
Location: USA
sure, read the doc, and learn to ask better question!

http://docs.jboss.org/hibernate/stable/ ... components

_________________
Zemian Deng
------------
Need a Java Scheduler? Try
http://bitbucket.org/timemachine/scheduler


Top
 Profile  
 
 Post subject: Re: use composite datatype with hibernate
PostPosted: Wed Oct 13, 2010 4:06 am 
Newbie

Joined: Sat Oct 09, 2010 1:58 am
Posts: 3
i have a composite type in postgres i want use this type in hibernate for example
Code:
create type Address as (
street varchar(100),
city varchar(100),
NO int
)

create table person(
id int,
name varchar(100),
address Address
)

is it possible?


Top
 Profile  
 
 Post subject: Re: use composite datatype with hibernate
PostPosted: Sat Jan 29, 2011 4:37 am 
Newbie

Joined: Sun Aug 23, 2009 8:27 pm
Posts: 3
I am trying to do the same thing, using PostgreSQL with JBoss Seam, but am receiving the following error:

Caused by: org.hibernate.MappingException: Could not determine type for: MyClass, at table: myTable, for columns: [org.hibernate.mapping.Column(myColumn)]

Did you eventually figure out how to do this?


Top
 Profile  
 
 Post subject: Re: use composite datatype with hibernate
PostPosted: Sun Feb 13, 2011 2:26 pm 
Newbie

Joined: Sat Oct 09, 2010 1:58 am
Posts: 3
no
i couldnt solve this problem
i want help
please anyone know how to solve it help me.


Top
 Profile  
 
 Post subject: Re: use composite datatype with hibernate
PostPosted: Sun Feb 13, 2011 9:59 pm 
Newbie

Joined: Sun Aug 23, 2009 8:27 pm
Posts: 3
azadeh,

I finally got this working. By the way, I did read the docs (particularly the hibernate annotations doc) and did not find the solution documented there. I am not claiming that this is the "correct solution". Rather, I am simply suggesting this based on what worked for me. If anyone has a "better" or "correct solution", then by all means.

I use Eclipse with JBoss AS and JBoss Seam but believe that the following should work for netbeans as well. I recommend the following:

First, in the postgresql database, include some kind of deliminator within the Address data because the solution that I propose reads the full address entry from the person table as one combined, composite string value. The Address class includes @Transient fields city and state that store the parsed city and state parts of the composite string. The parsing is done in the setter of the composite string field within the Address class. Thus for example, using brackets as a deliminator such as {1 Second Ave.}{New York} in the data in the person table, address column of postgresql.

import javax.persistence.Embedded;
@Entity
//Note: Postgresql requires names to be enclosed in `` marks if working with mixed case names
@Table(name="`person`")
public class Person implements Serializable //Definition Begin
{
private Address address;

@Embedded
public Address getAddress() { return address }

public void setAddress(Address address) { this.address = address; }

} //public class Person implements Serializable //Definition End


import javax.persistence.Embeddable;
@Embeddable
public class Address implements Serializable //Definition Begin
{
private String composite;

//I am not including the code for limiting to varchar(100). Thus, this is equivalent to postgresql text
private String street;
private String city;

//Note: in the @Column below, the name= refers to the address field in the Person class. The columnDefinition= refers to the Address type in Postgresql. Again, the `` marks are required because the A in Address is capitalized.
@Column(name="`address`", columnDefinition="`Address`")
public String getComposite() { return composite; }

public void setComposite(String composite) //Definition Begin
{
this.composite = composite;

//Write code here that will parse composite using the brackets {} deliminators such that if composite=="{1 Second Ave.}{New York}" then you set this.street="1 Second Ave." and this.city="New York".

} //public void setComposite(String composite) //Definition End


@Transient
public String getStreet {return street;}
public void setStreet(String street) {this.street = street;}


@Transient
public String getCity {return city;}
public void setCity(String city) {this.city = city;}

} //public class Address implements Serializable //Definition End

You can now access a person's street and city via: person.address.street and person.address.city

Hope that helps.

Regards,
Jeff


Top
 Profile  
 
 Post subject: Re: use composite datatype with hibernate
PostPosted: Thu Jul 05, 2012 7:50 am 
Newbie

Joined: Tue Jun 17, 2008 11:01 pm
Posts: 16
There is a simple example..
Hibernate Component Mapping Using Annotations

http://www.dzone.com/tutorials/java/hib ... ons-1.html


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 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.