0

I have a many-to-one relationship between two objects: SomeProjectType and Work Orders. In SomeProjectType, I have:

@OneToMany(mappedBy = "project", fetch = FetchType.EAGER)
private Set<WorkOrder> workOrders;

SomeProjectType has a "ProjectKey" as the @id for it.

And in WorkOrder I have:

@ManyToOne
@JoinColumn(name = "WorkOrderProjectKey")
private SomeProjectType project;

The issue I am having is that sometimes in WorkOrder, the "WorkOrderProjectKey" has a project key that doesn't exist in SomeProjectType (I am not sure why, but it is by design).

My question is: Is there a way to have Hibernate still return back rows even if some do not match? I have tried "nullable=true" and "optional=true" but it still won't work.

Ascalonian
  • 14,409
  • 18
  • 71
  • 103

2 Answers2

0

try to this code because i have same problem then i will change code and work properly.

Primary Key Tables

@OneToMany(mappedBy = "project")
private List<WorkOrder> workOrders;

Foreign Key Table

@ManyToOne
@JoinColumn(name = "WorkOrderProjectKey")
private SomeProjectType project;
  • Was there something to be different in the "then" part? Because it looks the same as I have above – Ascalonian Jul 13 '15 at 13:18
  • So there is no difference on the Foreign Key, just the Primary? Let me give this a try, thanks! – Ascalonian Jul 13 '15 at 13:22
  • When I try that, I get the following error: `org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags` – Ascalonian Jul 13 '15 at 13:27
0

I got it to work! Under the @ManyToOne, I put the following and it gets everything.

@NotFound( action = NotFoundAction.IGNORE )

Got this from the answer here: Hibernate chokes on missing rows when dealing with a legacy database

Community
  • 1
  • 1
Ascalonian
  • 14,409
  • 18
  • 71
  • 103