-->
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.  [ 2 posts ] 
Author Message
 Post subject: OneToMany instead of ManyToMany
PostPosted: Mon Dec 10, 2007 10:47 am 
Newbie

Joined: Mon Dec 10, 2007 10:21 am
Posts: 4
Hi,

I was having trouble with a ManyToMany mapping, so I decided to switch to using OneToMany with a third join class.

Eg. Option ---< OptionValue >--- Value


The problem occurs when I create a new Option and add a new Value.

Hibernate tries to save the Option (ok), then the OptionValue (bad).

It fails on the OptionValue because the Value does not exist yet.

So I get the standard "org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value..." error.

Can anyone tell me what I'm doing wrong?

The code details are below.

Thanks.


I am using hibernate annotations. Hibernate 3, Java 5, MySQL 5.

My Option class has something like:

@OneToMany( mappedBy="option", cascade= {CascadeType.ALL})
public List<OptionValue> getOptionValueList() {
return optionValueList;
}

public void setoptionValueList(List<OptionValue> optionValueList) {
this.optionValueList = optionValueList;
}

public void addValue(Value value) {
OptionValue join = new OptionValue();
join.setOption(this);
join.setValue(value);
optionValueList.add(join);
optionValue.getOptionValueList().add(join);
}

public void removeValue(Value value) {
for(OptionValue join : optionValueList) {
if(join.getValue().equals(value)) {
optionValueList.remove(join);
value.getOptionValueList().remove(join);
}
}
}





My Value class has something like:

@OneToMany( mappedBy="value", cascade= {CascadeType.ALL})
public List<OptionValue> getOptionValueList() {
return optionValueList;
}
public void setOptionValueList(List<OptionValue> optionValueList) {
this.optionValueList = optionValueList;
}

public void addOption(Option option) {
OptionValue join = new OptionValue();
join.setOption(option);
join.setValue(this);
optionValueList.add(join);
option.getoptionValueList().add(join);
}

public void removeOption(Option option) {
for(OptionValue join : optionValueList) {
if(join.getOption().equals(option)) {
optionValueList.remove(join);
option.getoptionValueList().remove(join);
}
}
}



And my OptionValue (join) class looks like:

@ManyToOne
@JoinColumn(name="optionId", nullable=false)
public Option getOption() {
return option;
}
public void setOption(Option option) {
this.option = option;
}

@ManyToOne
@JoinColumn(name="valueId", nullable=false)
public Value getValue() {
return value;
}
public void setValue(Value value) {
this.value = value;
}






Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

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

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 11, 2007 8:17 am 
Beginner
Beginner

Joined: Thu May 17, 2007 9:56 am
Posts: 21
Location: India
If i am right, i think the problem may be with the data you are trying to insert. You may need to check with your code in other parts that whether the value is getting passed correctly or not.

Because you will get "not null property references.." error while trying to insert a null value into the database column which is not nullable column.

_________________
Kuzhali


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