idbentley
idbentley

Reputation: 4218

Handling data exceptions with JPA 2 persistence

I'm using hibernate as JPA Persistence provider, and haven't been able to find any way to handle data exceptions in the documentation.

I have two entities:

@Entity
public class Item {
    @Id
    @GeneratedValue
    Long id;

    @ManyToOne
    @JoinColumn(name="node_id")
    Node node;
    .. snip ..
}

@Entity
public class Node {
    @Id
    @GeneratedValue
    Long id;

    @OneToMany
    @JoinColumn(name="node_id")
    List<Item> items;
    .. snip ..
}

In my database however, I have a data exception where there is an item row, with node_id = X but no node with id X.

I can't clean up this data for annoying, unrelated reasons.

Is there any way I can configure JPA to not explode when it hits this data?

Upvotes: 0

Views: 346

Answers (2)

Alex Barnes
Alex Barnes

Reputation: 7218

You could use the @NotFound(IGNORE) annotation on your property. Javadoc here

Upvotes: 1

Shivan Dragon
Shivan Dragon

Reputation: 15229

Well you can't stifle JPA's exception. You can however create an entity listener that listens for entity save/update, and caches and deals with any exceptions that may come from them:

http://docs.oracle.com/cd/B31017_01/web.1013/b28221/cmp30cfg015.htm

Upvotes: 0

Related Questions