• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@OneToMany problem with complex PK

 
Shannon McGee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting an oracle error when I go to delete an object Dog from the DB, stating that Bark.id cannot be updated to NULL. The DB is set up to perform a cascade
delete on items in the table "Bark" when the Dog item w/ Dog.ID = Bark.ID is deleted, and this functionality is working correctly.

Thoughts? Thank you!

@Entity
@Table(name="DOG")
public class Dog {
Long id;
List<Bark> barks;

@Generated(a sequence..)
@Id
@Column(ID)
public Long getId() {
return this.id;
}

@OneToMany
@JoinColumn(name="id")
public List<Bark> getBarks() {
return this.barks;
}

// setters, etc...

}

@Entity
@Table(name="BARK")
public class Bark {
BarkPk pk;

@Id
public BarkPk getPk() {
return this.pk;
}

// setters, etc..

}

@Embeddable
public class BarkPk implements Serializable {
Long id;
String volume;

// this ID is a FK to the ID in Dog
@Column(name="ID")
public Long getId() {
...
}

@Column(name="VOLUME")
public String getVolume() {
...
}

// setters, etc...
}
 
Shannon McGee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I am able to save data and retrieve it (including "bark" items associated with a given "dog" item).
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would try two different things.

First try

@OneToMany
@JoinColumn(name="id", updatable=false, insertable=false)
public List<Bark> getBarks() {
return this.barks;
}

and if that doesn't work set a Cascade option for the mapping

@OneToMany
@Cascade([ALL, DELETE-ORPHAN])
@JoinColumn(name="id")
public List<Bark> getBarks() {
return this.barks;
}

or just

@OneToMany
@Cascade([DELETE-ORPHAN])
@JoinColumn(name="id")
public List<Bark> getBarks() {
return this.barks;
}

and let us know if any of them work for you.

Good Luck

Mark
 
Shannon McGee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither of those suggestions seemed to resolve my problem, but I have made some progress...

I've found that if I set "Bark" to fetchType=EAGER, my error is not thrown at Bark, but at the Volume.

I can't make Bark and Volume fetch type EAGER, b/c that causes new issues (only one fetchType=EAGER allowed per model).

I know that with the LAZY fetch type, you must access the data from the sub-tables for it to be loaded, so I am doing that. I can see that the data has been loaded into the Bark/ Volume items before the delete is executed.

I appreciate the input!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic