4

I am new to Dropwizard and so far everything was going well till I started messing with Hibernate and MySQL. My problem is: Hibernate won't create tables and consequently no columns in my DB.

The only warning I get when running my jar file is:

org.hibernate.cfg.environment hibernate.properties not found

But do I need it at all? As I am having all configuration and mapping already.

Here is my application class:

    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));
    }

}

YAML file:

    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 class:

    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;
    }
}

and my entity:

@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;
    }
}

I have already been to many tutorials but none of them really explains how to configure hibernate. Thank you in advance.

CannotCode
  • 125
  • 1
  • 2
  • 10

1 Answers1

2

I have finally solved this problem, which was not a big deal actually. Just a small mistake as it was expected.

My problem was a Book class, IDE automatically imported the java library called Book in the LibraryApplication class, so DB was not mapping it.

On the other hand, in the Book class the named query should be as follows:

@NamedQuery(
name = "library.core.Book.findAll",
query = "SELECT b FROM Book b"
    )

My mistake: I was writing Book with small letter.

CannotCode
  • 125
  • 1
  • 2
  • 10