4

I have a problem with a OneToMany/ManyToOne relationship:

Class Project:

@OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL,
orphanRemoval=true )
@JoinColumn(name="PROJECT_ID", nullable=true)
private Set<Person> personlist = new HashSet<Person>();

Class Person:

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "PROJECT_ID") 
private Project project;

Everything works fine as long as there is at least one person connected to a project in the database. If I create a new project and there is no person in the database I get an hibernate exception:

org.hibernate.AssertionFailure: null identifier

I already set nullable=true for the project class but this doesnt work. Ideas anyone?

user1915440
  • 41
  • 1
  • 2

2 Answers2

0

Not sure if this helps try @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = true)

justMe
  • 2,200
  • 16
  • 20
  • How about @OneToMany(mappedBy = "project", fetch=FetchType.LAZY) private Set personlist = new HashSet(); and then in Person calss @ManyToOne() @JoinColumn(name = "PROJECT_ID") private Project project; – justMe Dec 19 '12 at 13:58
0

I've been looking all day for a solution to this problem. I have a solution / work around.

@OneToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL,
orphanRemoval=true )
@JoinColumn(name="PROJECT_ID", nullable=true)
private Set<Person> personlist = new HashSet<Person>();

I have a similar issue regarding foods and ingredients.

A food can exist without being an ingredient in a recipe. An ingredient can't exist without being a food. So we have a one to zero or many between ingredient and food:

RECIPE 1 ------------- 1..* INGREDIENT 0..* --------- 1 FOOD

Please excuse the rubbish representation above. I hope you get the idea.

When fetch type below is EAGER, I get the same issue as you. when it's set to lazy, I don't.

@NotFound(action = NotFoundAction.IGNORE)
@OneToMany(mappedBy = "food", // the VARIABLE NAME of this class in Ingredient
targetEntity = Ingredient.class, // The type that we have many of
fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<Ingredient> ingredients;

This isn't really a good solution, just a work around. I think an EAGER fetch should fetch without error even if there's nothing to fetch, then I'd have foods available and if they happen to be ingredients in recipes, then I'd have those ingredients availabel in my object, and through that, the recipes they are in. If they are not ingredients, just foods on their own, I still want the foods - it's just they'll have no ingredients associated no problem... but can't have them if this is the only work around

Does anyone have any better solutions to this? If you do you could save my life here :-)