2

I'm writing a new application which uses a old Database.
I have a Table named "score" looking like this:

+-----+------------+--------------------------+
| id  | name       | sub_score                |
+-----+------------+--------------------------+
| 205 | High Score |                      206 |
| 206 | Mid Score  |                      207 |
| 207 | Low Score  |                        0 |
+-----+------------+--------------------------+

My Model Entity to this is:

@Entity
@Table(name = "score")
public class ScoreGroups implements Serializable {

    private int id;
    private String name;
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, optional =   true)
    @JoinColumn(name = "haupt_kondition_id")
    @NotFound(action = NotFoundAction.IGNORE)
    private ScoreGroups sub_Score;
}

So I'm trying to do a selfjoin mapping id with sub_score but due to legacy Database antidesign, instead of null, a 0 is written, if a Score doesn't have a sub_score. Loading the Data via Hibernate produces a:

org.hibernate.ObjectNotFoundException: No row with the given identifier exists: 

Because Hibernate only ignores null values, not 0 with @NotFound.

How can a tell Hibernate to ignore the internal Object mapping if it finds a 0 in "sub_score"?

Mintri
  • 133
  • 5

1 Answers1

2

Try this

@JoinColumn(name = "haupt_kondition_id")
@Where(clause = "sub_score <> 0")
Reimeus
  • 158,255
  • 15
  • 216
  • 276