Hibernate base building

  • When using hibernate, a parameter can be applied hibernate.hbm2ddl.autowhich determines what to do with the base (create, update, etc.). I need the base to be built. I have the following code for this:

    //Сервис для работы с базой
    public class DBService {
        private SessionFactory sessionFactory;
    public DBService(Configuration configuration){
        sessionFactory = createSessionFactory(configuration);
    private static SessionFactory createSessionFactory(Configuration configuration){
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
        ServiceRegistry serviceRegistry = builder.build();
        return configuration.buildSessionFactory(serviceRegistry);


    @Entity(name = "user")
    @Table(name = "users")
    public class UserDataSet implements Serializable {
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "login")
    private String login;
    @Column(name = "password")
    private String password;
    @Column(name = "email")
    private String email;
    public UserDataSet(){}
    public UserDataSet(String login, String password, String email){
        this.login = login;
        this.password = password;
        this.email = email;


    And then, I'm still in the code asking for a pattern for a date service and making it:

    Configuration configuration = new Configuration()
    .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
    .setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
    .setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/somedb")
    .setProperty("hibernate.connection.username", "some_user")
    .setProperty("hibernate.connection.password", "some_password")
    .setProperty("hibernate.show_sql", "false")
    .setProperty("hibernate.hbm2ddl.auto", "create");

    dbService = new DBService(configuration);

    I think it's okay, everything has to work, but no. There's an exception, like the base doesn't exist. And if it's made by hands, it works.

    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'somedb'

    By logic, when installed hibernate.hbm2ddl.auto value createthe base must be created and everything must work, but it doesn't. What's the problem?

  • Gibernate doesn't create a base, it creates schemei.e. data structure in existing OBD. As you can see, you're putting the OBD on the JDBC URL, and the establishment of the base goes beyond the hyberneite-- the hybernate is operating inside the database. Anyway, you'll always have to create the OBD itself with your hands.

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2