dropwizard 连接mysql_mysql – Dropwizard Hibernate配置

我是Dropwizard的新手,到目前为止,一切顺利,直到我开始搞乱Hibernate和

MySQL.我的问题是:Hibernate不会创建表,因此我的数据库中没有列.

运行我的jar文件时唯一的警告是:

未找到org.hibernate.cfg.environment hibernate.properties

但我需要它吗?因为我已经拥有所有配置和映射.

这是我的应用程序类:

public class LibraryApplication extends Application {

public static void main(String[] args) throws Exception {

new LibraryApplication().run(args);

}

@Override

public String getName() {

return "hello backend";

}

private final HibernateBundle hibernate = new HibernateBundle(Book.class){ //more entities can be added separated with a coma

public DataSourceFactory getDataSourceFactory(LibraryConfiguration configuration) {

return configuration.getDataSourceFactory();

}

};

@Override

public void initialize(Bootstrap bootstrap) {

bootstrap.addBundle(new AssetsBundle("/webapp", "/", "index.html", "static"));

bootstrap.addBundle(hibernate);

}

@Override

public void run(LibraryConfiguration configuration,

Environment environment) {

final BookDAO dao = new BookDAO(hibernate.getSessionFactory());

final TestResource resource = new TestResource(

configuration.getTemplate(), configuration.getDefaultName());

final TemplateHealthCheck healthCheck = new TemplateHealthCheck(

configuration.getTemplate());

environment.healthChecks().register("template", healthCheck); //register the health check

environment.jersey().register(resource); //register the resource class

environment.jersey().register(new BookResource(dao));

}

}

YAML文件:

server:

type: simple

rootPath: '/api/*'

applicationContextPath: /

connector:

type: http

port: 8080

template: Hello, %s!

defaultName: back-end

database:

# the name of your JDBC driver

driverClass: com.mysql.jdbc.Driver

# the JDBC URL

url: jdbc:mysql://localhost:3306/books

# the username

user: root

# the password

password: root

# any properties specific to your JDBC driver:

properties:

charSet: UTF-8

hibernate.dialect: org.hibernate.dialect.MySQLDialect #org.hibernate.dialect.MySQL5InnoDBDialect

hibernate.hbm2ddl.auto: create

Configurtion类:

public class LibraryConfiguration extends Configuration{

@Valid

@NotNull

@JsonProperty

private DataSourceFactory database = new DataSourceFactory();

@JsonProperty("database")

public DataSourceFactory getDataSourceFactory() {

return database;

}

@NotEmpty

private String template;

@NotEmpty

private String defaultName = "";

@JsonProperty

public String getTemplate() {

return template;

}

@JsonProperty

public void setTemplate(String template) {

this.template = template;

}

@JsonProperty

public String getDefaultName() {

return defaultName;

}

@JsonProperty

public void setDefaultName(String name) {

this.defaultName = name;

}

}

和我的实体:

@Entity

@Table(name = "book")

@NamedQueries({

@NamedQuery(

name = "library.core.Book.findAll",

query = "SELECT b FROM book b"

)

})

public class Book{

@Id

@GeneratedValue(strategy=GenerationType.IDENTITY)

@Column

private Long id;

@Column(name = "title")

@NotNull

private String title;

@Column(name = "author")

@NotNull

private String author;

@Column(name = "date")

private long date;

@Column(name = "description")

private String description;

@Column(name = "image")

private String image;

public Book(String title, String author){

this.title = title;

this.author = author;

}

@JsonProperty

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

@JsonProperty

public Long getId() {

return id;

}

@JsonProperty

public String getAuthor() {

return author;

}

public void setAuthor(String author) {

this.author = author;

}

@JsonProperty

public long getDate() {

return date;

}

public void setDate(long date) {

this.date = date;

}

@JsonProperty

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

@JsonProperty

public String getImage() {

return image;

}

public void setImage(String image) {

this.image = image;

}

public void setId(Long id) {

this.id = id;

}

}

我已经去过很多教程,但没有一个真正解释如何配置hibernate.先感谢您.

最佳答案 我终于解决了这个问题,实际上这并不是什么大问题.只是一个小错误,因为它是预期的.

我的问题是Book类,IDE自动导入了LibraryApplication类中名为Book的java库,因此DB没有映射它.

另一方面,在Book类中,命名查询应如下所示:

@NamedQuery(

name = "library.core.Book.findAll",

query = "SELECT b FROM Book b"

)

我的错误:我正在用小写字母写书.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值