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?