启动程序(启动TomCat)后,模式中没有创建表,但必须自动创建表“player”。
我检查了休眠配置,但找不到问题出在哪里。我试过将www.example.com更改hbm2ddl.auto为hibernate.hbm2ddl.auto(也可以创建、创建-删除等),但没有帮助。
如果有什么想法,请告诉我。谢谢。
实体类:
package com.game.entity;
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(schema = "rpg", name = "player")
public class Player {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name", length = 12, nullable = false)
private String name;
@Column(name = "title", length = 30, nullable = false)
private String title;
@Column(name = "race", nullable = false)
@Enumerated(EnumType.ORDINAL)
private Race race;
@Column(name = "profession", nullable = false)
@Enumerated(EnumType.ORDINAL)
private Profession profession;
@Column(name = "birthday", nullable = false)
private Date birthday;
@Column(name = "banned", nullable = false)
private Boolean banned;
@Column(name = "level", nullable = false)
private Integer level;
public Player() {
}
public Player(Long id, String name, String title, Race race, Profession profession, Date birthday, Boolean banned, Integer level) {
this.id = id;
this.name = name;
this.title = title;
this.race = race;
this.profession = profession;
this.birthday = birthday;
this.banned = banned;
this.level = level;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Race getRace() {
return race;
}
public void setRace(Race race) {
this.race = race;
}
public Profession getProfession() {
return profession;
}
public void setProfession(Profession profession) {
this.profession = profession;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Boolean getBanned() {
return banned;
}
public void setBanned(Boolean banned) {
this.banned = banned;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
}
存储库类:
package com.game.repository;
import com.game.entity.Player;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.NativeQuery;
import org.springframework.stereotype.Repository;
import javax.annotation.PreDestroy;
import java.util.List;
import java.util.Optional;
@Repository(value = "db")
public class PlayerRepositoryDB implements IPlayerRepository {
private final SessionFactory sessionFactory;
public PlayerRepositoryDB() {
Configuration configuration = new Configuration().configure().addAnnotatedClass(Player.class);
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
@Override
public List<Player> getAll(int pageNumber, int pageSize) {
try(Session session = sessionFactory.openSession()){
NativeQuery<Player> nativeQuery = session.createNativeQuery("SELECT * FROM rpg.player", Player.class);
nativeQuery.setFirstResult(pageNumber * pageSize);
nativeQuery.setMaxResults(pageSize);
return nativeQuery.list();
}
}
休眠配置:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/rpg</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
可通过以下链接获得包含pom.xml的完整项目代码:https://github.com/gamlethot/project-hibernate-1
1条答案
按热度按时间pxyaymoc1#
1-Hibernate无法识别您的存储库。您不应该将存储库类标记为
@Repository
,因为它们不是接口,在您的示例中,它们像服务一样工作。因此它们可以是@Service
。2-不要实现
IPlayerRepository
。将其标记为@Repository
,并将其自动连接到服务类(或者使用构造函数注入,并像变量一样使用),例如:3-DB资料档案库类正在实现
IPlayerRepository
,但必须将其标记为@Repository
,并且它应扩展CrudRepository
或JpaRepository
(已扩展CrudRepository)。比如:
这里,
Long
是Player
类的主键类型。