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