There was a problem in the application using Hibernate when changing the database from MySQL to PostgreSQL. With the connected MySQL, the code worked as it should (except for some nuances), but when I changed the Hibernate settings, the code began to crash. So in order:
General question. How to make it so that in the method persistNewUser (User user); It was possible to verify the uniqueness of the login with the existing table, and not to check if the table does not exist yet.
̶V̶y̶l̶e̶t̶a̶e̶t̶ ̶o̶sh̶i̶b̶k̶a̶ ̶o̶r̶g̶.̶h̶i̶b̶e̶r̶n̶a̶t̶e̶.̶e̶x̶c̶e̶p̶t̶i̶o̶n̶.̶S̶Q̶L̶G̶r̶a̶m̶m̶a̶r̶E̶x̶c̶e̶p̶t̶i̶o̶n̶: ̶ ̶c̶o̶u̶l̶d̶ ̶n̶o̶t̶ ̶e̶x̶e̶c̶u̶t̶e̶ ̶s̶t̶a̶t̶e̶m̶e̶n̶t̶ ̶v̶ ̶m̶e̶t̶o̶d̶e̶ ̶c̶l̶o̶s̶e̶C̶u̶r̶r̶e̶n̶t̶S̶e̶s̶s̶i̶o̶n̶W̶i̶t̶h̶T̶r̶a̶n̶s̶a̶c̶t̶i̶o̶n̶ (̶) ̶ ̶p̶r̶i̶ ̶k̶o̶m̶i̶t̶e̶.̶
̶T̶a̶k̶ ̶zh̶e̶ ̶v̶y̶l̶e̶t̶a̶e̶t̶ ̶o̶sh̶i̶b̶k̶a̶ ̶o̶r̶g̶.̶p̶o̶s̶t̶g̶r̶e̶s̶q̶l̶.̶u̶t̶i̶l̶.̶P̶S̶Q̶L̶E̶x̶c̶e̶p̶t̶i̶o̶n̶: ̶ ̶E̶R̶R̶O̶R̶: ̶ ̶s̶y̶n̶t̶a̶x̶ ̶e̶r̶r̶o̶r̶ ̶a̶t̶ ̶o̶r̶ ̶n̶e̶a̶r̶ ̶ "̶u̶s̶e̶r̶" ̶ ̶ ̶ ̶P̶o̶z̶i̶ts̶i̶ya̶: ̶ ̶1̶3̶
UPD: 2 and 3 decided to just ... even though I ruffled with them, the table cannot be called user in Postgre
Why without calling the configuration.addAnnotatedClass (User.class) method; doesn't find the entity User class, even considering the fact that in cfg zamapil it?
Please tell me how to solve these problems, thank you.
for a start config
<hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</property> <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres</property> <property name="hibernate.connection.username">postgres</property> <property name="hibernate.connection.password">root</property> <property name="current_session_context_class">thread</property> <property name="hibernate.show_sql">true</property> <property name="hbm2ddl.auto">update</property> <property name="show_sql">true</property> <mapping class="entity.User"/> </session-factory> Entity
@Entity @Table(name = "user") public class User { @Id private String login; private String password; public User() { } public User(String login, String password) { this.login = login; this.password = password; } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } DAO
public class UserDaoImpl implements UserDao<User, String> { private Session currentSession; private Transaction currentTransaction; public UserDaoImpl() { } public Session openCurrentSession() { currentSession = getSessionFactory().openSession(); return currentSession; } public Session openCurrentSessionWithTransaction() { currentSession = getSessionFactory().openSession(); currentTransaction = currentSession.beginTransaction(); return currentSession; } public void closeCurrentSession() { currentSession.close(); } public void closeCurrentSessionWithTransaction() { currentTransaction.commit(); currentSession.close(); } private static SessionFactory getSessionFactory() { Configuration configuration = new Configuration().configure(); configuration.addAnnotatedClass(User.class); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()); return configuration.buildSessionFactory(builder.build()); } public Session getCurrentSession() { return currentSession; } public void setCurrentSession(Session currentSession) { this.currentSession = currentSession; } public Transaction getCurrentTransaction() { return currentTransaction; } public void setCurrentTransaction(Transaction currentTransaction) { this.currentTransaction = currentTransaction; } public void persist(User user) { getCurrentSession().save(user); } public User findUserByLogin(String login) { return getCurrentSession().get(User.class,login); } } Service
public class AuthenticationService { private static UserDaoImpl userDao; public AuthenticationService(){ userDao = new UserDaoImpl(); } public void persistNewUser(User user){ userDao.openCurrentSessionWithTransaction(); Query query = userDao.getCurrentSession().createQuery("select count(login)" + " from User where login='"+user.getLogin()+"'"); Long count = (Long)query.uniqueResult(); if (count==0){ userDao.persist(user); } userDao.closeCurrentSessionWithTransaction(); } public boolean checkUserData(String login, String password){ userDao.openCurrentSession(); User user = userDao.findUserByLogin(login); userDao.closeCurrentSession(); return user.getPassword().equals(password); } }