I have two tables with @OneToOne by a id.
So if the second id refer to a not present row of the table
can I say to Hibernate to skyp this row.
Instead I have an error.
@Entity
@Table(name="user")
public class User implements Serializable {
private Long idUser;
private Area area;
//...other get and setter
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name="idarea")
public Area getArea() {
return area;
}
}
now if I have an row of user with non-existent idarea , how can I skip the user,
or can I make a LEFT JOIN?
This situation should not happen in a well-defined schema, with foreign key constraint. If the user has a non-null
idarea, then this ID should refer to an existing ID in the Area table. I would fiw the data, and add a foreign key constraint to make sure this never happens.If for extremely good reasons, it’s impossible, then you can use the
@NotFound(NotFoundAction.IGNORE)annotation. But it will have a performance cost, because Hibernate will have to check i the ID is a real ID or a non-existing ID each time it loads a user.