-->
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.  [ 7 posts ] 
Author Message
 Post subject: Embedding an Object
PostPosted: Sat Dec 02, 2006 11:01 pm 
Newbie

Joined: Sat Dec 02, 2006 10:55 pm
Posts: 10
Hey there

im just starting to use Hibernate and im having trouble understanding how to embedd an object in my class:


for eg:

Code:
@Entity
public Class Person{


long id;
Pet pet;

@Id
public long getId(){
}

// generated getters and setters for all other class attributes

}



Code:
public Class Pet{

String petName
// generated getters and setters for all other class attributes
}


Top
 Profile  
 
 Post subject: Embedding an Object
PostPosted: Sun Dec 03, 2006 12:41 am 
Beginner
Beginner

Joined: Thu Apr 27, 2006 12:19 pm
Posts: 33
Location: Seattle, WA
Basically you need to map Pet with an id, and then use a many-to-one in Person. The generated Person table should have a foreign key to Pet.

@Entity
public Class Person{

long id;
Pet pet;

@Id
public long getId()
{
return id;
}

public void setId(long id)
{
this.id = id;
}

@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
public Pet getPet()
{
return pet;
}

public void setPet(Pet pet)
{
this.pet = pet;
}

}

@Entity
public Class Pet{

long id;
String petName

@Id
public long getId()
{
return id;
}

public void setId(long id)
{
this.id = id;
}

public String getPetName()
{
return petName;
}

public void setPetName(String petName)
{
this.petName = petName;
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 03, 2006 3:39 pm 
Newbie

Joined: Sat Dec 02, 2006 10:55 pm
Posts: 10
thankx


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 03, 2006 3:45 pm 
Newbie

Joined: Sat Dec 02, 2006 10:55 pm
Posts: 10
thanks for that. However, does using annotations imply that the DB table has to be physically created as opposed to parsing the XML config, hbm files which in turn automatically generate the DB relation


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 03, 2006 6:26 pm 
Newbie

Joined: Sat Dec 02, 2006 10:55 pm
Posts: 10
yep got it working


Top
 Profile  
 
 Post subject: Embedding an Object
PostPosted: Sun Dec 03, 2006 6:29 pm 
Beginner
Beginner

Joined: Thu Apr 27, 2006 12:19 pm
Posts: 33
Location: Seattle, WA
Annotations are just an alternative to using hbm.xml mapping files. In either case you can still have Hibernate generate the database schema for you. The hbm.xml mapping files have the advantage of being more widely used, thus you'll see more examples using these.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 03, 2006 6:35 pm 
Newbie

Joined: Sat Dec 02, 2006 10:55 pm
Posts: 10
yeah but its quite horrible to have to continually make changes every time something within the class changes, hence, if the project is very large then the xml files can slow things down


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