Mysql Dropwizard休眠配置
我是Dropwizard的新手,到目前为止一切都很顺利,直到我开始使用Hibernate和MySQL。我的问题是:Hibernate不会创建表,因此数据库中没有列 运行jar文件时得到的唯一警告是:Mysql Dropwizard休眠配置,mysql,hibernate,dropwizard,java-ee-7,Mysql,Hibernate,Dropwizard,Java Ee 7,我是Dropwizard的新手,到目前为止一切都很顺利,直到我开始使用Hibernate和MySQL。我的问题是:Hibernate不会创建表,因此数据库中没有列 运行jar文件时得到的唯一警告是: org.hibernate.cfg.environment hibernate.properties未找到 但我需要它吗?因为我已经有了所有的配置和映射 这是我的应用程序类: public class LibraryApplication extends Application<Lib
org.hibernate.cfg.environment hibernate.properties未找到
但我需要它吗?因为我已经有了所有的配置和映射
这是我的应用程序类:
public class LibraryApplication extends Application<LibraryConfiguration> {
public static void main(String[] args) throws Exception {
new LibraryApplication().run(args);
}
@Override
public String getName() {
return "hello backend";
}
private final HibernateBundle<LibraryConfiguration> hibernate = new HibernateBundle<LibraryConfiguration>(Book.class){ //more entities can be added separated with a coma
public DataSourceFactory getDataSourceFactory(LibraryConfiguration configuration) {
return configuration.getDataSourceFactory();
}
};
@Override
public void initialize(Bootstrap<LibraryConfiguration> 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));
}
}
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;
}
}
配置类:
public class LibraryApplication extends Application<LibraryConfiguration> {
public static void main(String[] args) throws Exception {
new LibraryApplication().run(args);
}
@Override
public String getName() {
return "hello backend";
}
private final HibernateBundle<LibraryConfiguration> hibernate = new HibernateBundle<LibraryConfiguration>(Book.class){ //more entities can be added separated with a coma
public DataSourceFactory getDataSourceFactory(LibraryConfiguration configuration) {
return configuration.getDataSourceFactory();
}
};
@Override
public void initialize(Bootstrap<LibraryConfiguration> 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));
}
}
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"
)
我的错误是:我在用小写字母写书