Dropwizard Hibernate Unable to locate persister Error


Dropwizard Hibernate Unable to locate persister Error
I have set up a Dropwizard project and my first endpoint for a baseball stats api. The functioning table and class names are rangerstats_player / Player. I tried to do the same for a table I have called rangerstats_hitter_season_stats but I run into the error below when trying to do a simple GET and have not figured out how to fix it or what the problem is
org.hibernate.UnknownEntityTypeException: Unable to locate persister: core.HitterSeasonStats
Below is the code for both classes/tables to show the similarities since I think the pattern is exactly the same and I have no idea what could be wrong.
HitterSeasonStats.java (commented out some fields to try to get it to work in case they might be the problem)
@Entity
@Table(name = "rangerstats_hitter_season_stats")
public class HitterSeasonStats
@Id
private long id;
// @Column(name = "player_id")
// private long playerId;
private int g;
private int pa;
private int ab;
//private double avg;
private int h;
private int single;
@Column(name = "double")
private int doubles;
private int triple;
private int hr;
private int rbi;
private int bb;
private int k;
private int hbp;
private int sf;
// private double slg;
// private double obp;
// private double ops;
// private double war;
//@Column(name = "season_year")
//private Date seasonYear;
public long getId()
return id;
HitterSeasonStatsDAO.java
public class HitterSeasonStatsDAO extends AbstractDAO<HitterSeasonStats>
public HitterSeasonStatsDAO(SessionFactory factory)
super(factory);
public Optional<HitterSeasonStats> findById(Long id)
return Optional.fromNullable(get(id));
public List<HitterSeasonStats> findByPlayer(Long playerId)
Criteria criteria = criteria().add(Restrictions.eq("playerId", playerId));
return list(criteria);
public List<HitterSeasonStats> findByPlayerAndYear(Long playerId)
Criteria criteria = criteria().add(Restrictions.eq("playerId", playerId));
return list(criteria);
Player.java
@Entity
@Table(name = "rangerstats_player")
@NamedQueries(
@NamedQuery(name = "core.Player.findAll",
query = "select p from Player p")
)
public class Player
@Id
private long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
private String position;
private String team;
private String bat;
@Column(name = "throw_hand")
private String throwHand;
private String height;
private int age;
private int weight;
public long getId()
return id;
public String getfirstName()
return firstName;
public String getlastName()
return lastName;
......
PlayerDAO.java
public class PlayerDAO extends AbstractDAO<Player>
public PlayerDAO(SessionFactory factory)
super(factory);
public Optional<Player> findById(Long id)
return Optional.fromNullable(get(id));
public List<Player> findAll()
return list(namedQuery("core.Player.findAll"));
Tables
rangersstats=# dt
List of relations
Schema | Name | Type | Owner
--------+----------------------------------+-------+---------------
public | rangerstats_hitter_game_record | table | owner
public | rangerstats_hitter_season_stats | table | owner
public | rangerstats_pitcher_game_record | table | owner
public | rangerstats_pitcher_season_stats | table | owner
public | rangerstats_player | table | owner
EDIT:
I believe I have found what is causing the issue. It is how I am registering the HibernateBundle. I have put this together form tutorials and have taken syntax from them. I see I am only using the Player class in my bundle. I need to find a new way to hook up the database in the main Application class.
private final HibernateBundle<ApiConfig> hibernatePlayerBundle =
new HibernateBundle<ApiConfig>(Player.class)
@Override
public DataSourceFactory getDataSourceFactory(ApiConfig configuration)
return configuration.getDataSourceFactory();
;
@Override
public void initialize(final Bootstrap<ApiConfig> bootstrap)
bootstrap.addBundle(hibernatePlayerBundle);
@Override
public void run(final ApiConfig configuration, final Environment environment)
final PlayerDAO playerDAO = new PlayerDAO(hibernatePlayerBundle.getSessionFactory());
final HitterSeasonStatsDAO hitterSeasonStatsDAO = new HitterSeasonStatsDAO(hibernateHitterSeasonStatsBundle.getSessionFactory());
environment.jersey().register(new HitterSeasonStatsResource(hitterSeasonStatsDAO));
environment.jersey().register(new PlayerResource(playerDAO));
I tried to create and add another bundle with the other class but it looks like we can only have one bundle
java.lang.IllegalArgumentException: A metric named io.dropwizard.db.ManagedPooledDataSource.hibernate.active already exists
EDIT:
I need to passed a comma separated list of classes to the HibernateBundle constructor, problem solved! :)
1 Answer
1
To not having such problems in the future, use ScanningHibernateBundle
.
ScanningHibernateBundle
ScanningHibernateBundle
will scan core
package and all nested sub packages and add all classes with @Entity
annotation to the Hibernate SessionFactory
.
ScanningHibernateBundle
core
@Entity
SessionFactory
private final HibernateBundle<ApiConfig> hibernatePlayerBundle =
new ScanningHibernateBundle<ApiConfig>("core")
@Override
public DataSourceFactory getDataSourceFactory(ApiConfig configuration)
return configuration.getDataSourceFactory();
;
Better to use core.model
package name in place of core
.
core.model
core
@NewDev You are welcome.
– v.ladynev
Nov 8 '17 at 13:32
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Excellent, thank you!
– NewDev
Nov 5 '17 at 21:59