我已经设置了一个Dropwizard项目和我的棒球统计api的第一个端点。功能表和类名是rangerstats_player / Player。我试图对我称为rangerstats_hitter_season_stats的表做同样的事情,但是当我尝试做一个简单的GET并且没有弄清楚如何解决它或问题是什么时我遇到了以下错误
org.hibernate.UnknownEntityTypeException: Unable to locate persister: core.HitterSeasonStats
以下是两个类/表的代码,以显示相似之处,因为我认为模式完全相同,我不知道可能出现什么问题。
HitterSeasonStats.java(注释掉一些字段以试图让它工作以防它们可能出现问题)
@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"));
}
}
表
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
编辑:
我相信我找到了造成这个问题的原因。这是我注册HibernateBundle的方式。我把它们放在一起形成教程,并从中获取了语法。我看到我只在我的包中使用了Player类。我需要找到一种新方法来连接主Application类中的数据库。
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));
}
我尝试使用另一个类创建并添加另一个包,但看起来我们只能有一个包
java.lang.IllegalArgumentException: A metric named io.dropwizard.db.ManagedPooledDataSource.hibernate.active already exists
编辑:
我需要将一个逗号分隔的类列表传递给HibernateBundle构造函数,问题解决了! :)
答案 0 :(得分:2)
如果将来不再出现此类问题,请使用ScanningHibernateBundle
。
ScanningHibernateBundle
将扫描core
包和所有嵌套的子包,并将所有带@Entity
注释的类添加到Hibernate SessionFactory
。
private final HibernateBundle<ApiConfig> hibernatePlayerBundle =
new ScanningHibernateBundle<ApiConfig>("core") {
@Override
public DataSourceFactory getDataSourceFactory(ApiConfig configuration) {
return configuration.getDataSourceFactory();
}
};
最好使用core.model
包名取代core
。