-->
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.  [ 4 posts ] 
Author Message
 Post subject: Foreign key value null if nullable true in @OneToMany assoc
PostPosted: Sun Feb 08, 2009 3:51 am 
Newbie

Joined: Mon Feb 02, 2009 12:41 pm
Posts: 5
public class Person

{

protected int id;

protected int version;

private List<Address> addresses = new ArrayList<Address>();

@Id
@Column(name = "Id", insertable = false, updatable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}

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

@Version
@Column(name = "Version")
public long getVersion() {
return version;
}

public void setVersion(long version) {
this.version = version;
}


@OneToMany(fetch = FetchType.LAZY)
@Cascade( { org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
@ForeignKey(name = "FK_ADDRESS")
@Index(name = "IDX_ADDRESS")
@JoinColumn(name = "PersonId", nullable = true, insertable = true, updatable = false)
public List<Address> getAddresses()
{
return addresses;
}


public void setAddresses(List<Address> address)
{
addresses = address;
}

}



public class Address

{

protected int id;

protected int version;

private String place;

//other properties

@Id
@Column(name = "Id", insertable = false, updatable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}

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

@Version
@Column(name = "Version")
public long getVersion() {
return version;
}

public void setVersion(long version) {
this.version = version;
}


@Column(name = "PersonId")
public String getPlace()
{
return addresses;
}


public void setPlace(String placeName)
{
place = placeName;
}

}


List<Address> addresses = new ArrayList<Address>();
Address address1 = new Address();
addresss.setPlace("XYZ");
addresses.add(address1);
Person person = new Person();
person.setAddresses(addresses);
HibernateTemplate template = getHibernateTemplate();
template.save(addresses);


When i do this operation, entries are getting creating in both tables. But problem, PersonId which is foreign key column in
address table is having value as null.

I am not getting what is the problem with my domain model. If i declare JoinColumn as nullable as false it is working fine . But my requirement is it can be null and if we provide some address, it should have
personId. Please tell me what is wrong with my domain class?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 08, 2009 4:05 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
I think there is some issue with your Address class mapping:
Code:
@Column(name = "PersonId")
public String getPlace()
{
return addresses;
}

Here you are mapping the place attribute to PersonId column. Is this right?

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 08, 2009 4:57 am 
Newbie

Joined: Mon Feb 02, 2009 12:41 pm
Posts: 5
Sorry... I pasted the code wrong. It is as below.

@Column(name = "Place")
public String getPlace()
{
return addresses;
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2009 12:46 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Thn is ur code having mapping for personId as well? Can you post the complete new code?

_________________
Regards,
Litty Preeth


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