When working with a database using hibernate, you can set the hibernate.hbm2ddl.auto parameter, which determines what to do with the database (create, update, etc.). I need the base itself to be created. I have the following code written 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(); builder.applySettings(configuration.getProperties()); ServiceRegistry serviceRegistry = builder.build(); return configuration.buildSessionFactory(serviceRegistry); } } //Датасет @Entity(name = "user") @Table(name = "users") public class UserDataSet implements Serializable { @Id @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; } } 

Well, then I somewhere else in the code set the configuration for the database service and create it:

 Configuration configuration = new Configuration() .addAnnotatedClass(UserDataSet.class) .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); 

It seems that everything is fine, everything should work, but no. The exception takes off, type the base does not exist. And if you create it with your hands, then everything works.

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

Logically, when hibernate.hbm2ddl.auto is set to create , the base should be created and everything should work, but it does not. What could be the problem?

    2 answers 2

    Hibernate does not create a base, it creates a circuit , i.e. data structure in an existing database. As you can see, you specify the database already in the JDBC URL, and the creation of the database goes beyond the responsibility of the hibernate — the hibernate is already operating inside the database. One way or another, you will always need to create the database itself with your hands.

      The answer may be delayed, but add this at the end of hibernate.connection.url ?createDatabaseIfNotExist=true .

      • What does it mean ? before createDatabaseIfNotExist ? - 0xdb
      • @ 0xdb What additional connection parameters will be indicated. For example, in addition to the above parameter, you can disable SSL by writing useSSL=false . Several parameters are <url>?useSSL=false&createDatabaseIfNotExsist=true with the & sign, for example: <url>?useSSL=false&createDatabaseIfNotExsist=true - Stas Dorozhko
      • Thank you, everything is so complicated in this world. You can post a question and specify the full line of code. - 0xdb
      • To be honest, I don’t know what you mean by "fix the question" (I'm new here), but the full line of code will be like this: setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/somedb?createDatabaseIfNotExsits=true") or in the hibernate.cfg.xml file write: <property name="connection.url">jdbc:mysql://localhost:3306/somedb?createDatabaseIfNotExist=true</property> - Stas Dorozhko
      • Sorry, I meant to correct the answer - 0xdb