-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: EntityNotFoundException for object with id that exists in DB
PostPosted: Sat Oct 15, 2016 11:43 pm 
Newbie

Joined: Sat Oct 15, 2016 9:49 pm
Posts: 5
Hi,

I have an Entity with many persistent fields two of which have a ManyToOne relationship of the same type. Abstractly the entities are defined similarly to the code below.

Code:
@Entity
@Table(name="a_table")
public class A implements Serializable {
    @Id
    @TableGenerator(
        name="a_generator",
        table="id_sequence",
        pkColumnName="name",
        pkColumnValue="a",
        valueColumnName="number"
    )
    @GeneratedValue(strategy=GenerationType.TABLE, generator="a_generator")
    private Integer id;

    /* Rest of the class */
}

@Entity
@Table(name="b_table")
@NamedQuery(name="B.findBySAndA1", query="SELECT b from B b WHERE b.s1 = :s AND b.a1 = :a")
public class B implements Serializable {
    @Id
    @TableGenerator(
        name="b_generator",
        table="id_sequence",
        pkColumnName="name",
        pkColumnValue="b",
        valueColumnName="number"
    )
    @GeneratedValue(strategy=GenerationType.TABLE, generator="b_generator")
    private Integer id;

    @ManyToOne
    @JoinColumn(name="a1")
    private A a1;

    @ManyToOne
    @JoinColumn(name="a2")
    private A a2;

    @Column(name="some_other_field")
    private String s1

    /* Rest of the class */
}


When I call the query
Code:
List<B> result = entityManager.findBySAndA1(someA, someS1);


I get an error:
EntityNotFoundException: Unable to find A with id X

Where X is the id of the A entity that corresponds to a2 for the corresponding someA entity. This object exists in the database and I have no problem retrieving the entity individually using its id.

I've found some similar questions and discussions here which suggest lazily loading a1 and a2. While this allows the query to retrieve the B object it just delays the error until I actually try to access the A objects.

I've tried with Hibernate versions 5.2.1, 5.2.3 and 5.1.2 which all give the same error. Any ideas on what I'm doing wrong?
Any help is appreciated.


Top
 Profile  
 
 Post subject: Re: EntityNotFoundException for object with id that exists in DB
PostPosted: Mon Oct 17, 2016 5:25 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
There is no method "findBySAndA1" in EntityManager. Post the actual code that you are executing.


Top
 Profile  
 
 Post subject: Re: EntityNotFoundException for object with id that exists in DB
PostPosted: Tue Oct 18, 2016 7:53 pm 
Newbie

Joined: Sat Oct 15, 2016 9:49 pm
Posts: 5
Yes, sorry. I was originally trying to do this with spring-data and when that failed I fell back to trying to do it with standard JPA and I mixed them up in the sample. I'll cut and paste some snippets from my actual code to try to avoid those kinds of mistakes again. To give a little more context I am trying to model sports teams and try to find games containing a specific player prior to a given date.

The (hopefully) relevant portions of the Entity classes
Code:
@Entity
@Table(name="team")
public class Team implements Serializable, Comparable<Team> {
    private static final long serialVersionUID = 1L;

    @Id
    @TableGenerator(
        name="team_generator",
        table="id_sequence",
        pkColumnName="name",
        pkColumnValue="team",
        valueColumnName="number"
    )
    @GeneratedValue(strategy=GenerationType.TABLE, generator="team_generator")
    private Integer id;

    @Column(name="name", length=180, nullable=false)
    private String name;

    /* Other fields and methods */
}

Code:
@Entity
@Table(name="game")
@NamedQueries({
    @NamedQuery(
        name="Game.findByDateAndTeam",
        query="SELECT g FROM Game g WHERE g.gameDate = :date AND (g.homeTeam = :team OR g.awayTeam = :team)"
    ),
    @NamedQuery(
        name="Game.findPreviousGames",
        query="SELECT g FROM Game g, Player p, BoxScoreRow b WHERE g.gameDate < :date AND g = b.game AND p = b.player AND p = :player ORDER BY g.gameDate DESC"
    )
})
public class Game implements Serializable, Comparable<Game> {
    @Id
    @Column(name="id")
    @TableGenerator(
        name="game_generator",
        table="id_sequence",
        pkColumnName="name",
        pkColumnValue="game",
        valueColumnName="number"
    )
    @GeneratedValue(strategy=GenerationType.TABLE, generator="game_generator")
    private Integer id;

    @Column(name="game_date", nullable=false)
    private LocalDate gameDate;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="home_team")
    private Team homeTeam;
   
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="away_team")
    private Team awayTeam;

    @OneToMany(mappedBy="game", cascade=CascadeType.ALL)
    private List<BoxScoreRow> boxScore;

    /* Other fields and methods */
}

Code:
@Entity
@Table(name="box_score")
public class BoxScoreRow implements Serializable {
    private static final long serialVersionUID = 1L;

    @Embeddable
    public static class BoxScoreRowPK implements Serializable {
        private static final long serialVersionUID = 1L;

        private int game;
        private int player;

        /* Other fields and methods */
    }

    @EmbeddedId
    private BoxScoreRowPK id;
   
    @ManyToOne
    @JoinColumn(name="game", insertable=false, updatable=false)
    private Game game;

    @ManyToOne
    @JoinColumn(name="player", insertable=false, updatable=false)
    private Player player;

    /* Other fields and methods */
}


Then to the code that throws the exception:
Code:
@Transactional
@Test
public void testFindPreviousGame() {
    Team homeTeam = /* An attached team retrieved from the DB */
    Player player = /* An attached player from the DB */
    LocalDate date = LocalDate.of(2015, 8, 1);
    Game game =
        entityManager.createNamedQuery("Game.findByDateAndTeam", Game.class)
            .setParameter("date", date)
            .setParameter("team", homeTeam)
            .getSingleResult();
       
    List<Game> previousGames =
        entityManager.createNamedQuery("Game.findPreviousGames", Game.class)
            .setParameter("date", date)
            .setParameter("player", player)
            .setMaxResults(5)
            .getResultList();
}


The exception is
Code:
javax.persistence.EntityNotFoundException: Unable to find Team with id 571

And yet there is a Team with id 571 in the database.


Top
 Profile  
 
 Post subject: Re: EntityNotFoundException for object with id that exists in DB
PostPosted: Wed Oct 19, 2016 4:58 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
It's curious you get an exception for the Team entity since you are fetching Game. I can't spot any issue in your mapping, so you'll have to debug the problem to see the root cause.


Top
 Profile  
 
 Post subject: Re: EntityNotFoundException for object with id that exists in DB
PostPosted: Wed Oct 19, 2016 12:55 pm 
Newbie

Joined: Sat Oct 15, 2016 9:49 pm
Posts: 5
Ok, I think I found the problem. It turns out some of the other "irrelevant" fields weren't so irrelevant after all. For some reason I had marked an optional @ManyToOne relation as not optional and so Hibernate was using an inner join instead of a left join causing it to fail to find the Team even though the id was present.

Thanks for the help despite my poorly formulated post(s).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.