我正在尝试使用 spring boot-jpa-hibernate 将数据保存到数据库。
这是我的 application.properties:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/BlurAdmin
spring.datasource.username=blurAdmin
spring.datasource.password =mypassword
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
c3p0.acquireIncrement=2
c3p0.minPoolSize=10
c3p0.maxPoolSize=20
c3p0.maxIdleTime=5000
c3p0.maxStatementsPerConnection=50
c3p0.idle_test_period=30000
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=update
这是 HibernateConfiguration.java:
@Configuration
public class HibernateConfiguration {
@Bean
public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
HibernateJpaSessionFactoryBean factory = new HibernateJpaSessionFactoryBean();
factory.setEntityManagerFactory(emf);
return factory;
}
}
这是我的型号: @Entity旁边有一条警告说:
项目不包含持久单位
@Entity
@Table(name = "BlurAdminUsers")
public class User implements Serializable{
public static final String ROLE_USER = "ROLE_USER";
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String username;
private String email;
private String password;
private String role;
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
protected User(){
}
public User(String username,String email,String password){
this.username = username;
this.password = password;
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
这是存储库:
@Transactional
public interface UsersRepository extends JpaRepository<User, Long> {
List<User> findByEmail(String email);
List<User> findByUsername(String username);
}
这是我的 aop类,我试图保留数据:
@Component
public class Signup {
private DataValidation dataValidation;
private Message message;
private UsersRepository repository;
@Autowired
public Signup(UsersRepository repository) {
dataValidation = new DataValidation();
this.repository = repository;
}
@Around("execution(* com.ghewareunigps.vts.ui.web.controllers.SignupController.signup(..)) && args(user,..)")
public Message validateAndSignup(ProceedingJoinPoint joinPoint, User user) throws Throwable {
List<User> usersWithEmail = repository.findByEmail(user.getEmail());
List<User> usersWithUsername = repository.findByUsername(user.getUsername()) ;
user.setRole(Roles.ROLE_USER);
if (dataValidation.validate(user.getUsername(), user.getEmail(), user.getPassword())) {
if(usersWithEmail.isEmpty() && usersWithUsername.isEmpty()){
message = (Message) joinPoint.proceed();
try{
repository.save(user);
}catch(Exception ex){
System.out.println(ex);
}
}
else{
message = new Message(false,"Sorry user with email or username already exists");
}
} else {
message = new Message(false, " please recheck your credentials");
}
return message;
}
}
没有抛出任何异常,我添加了所有依赖项。有人可以告诉我哪里出错了吗?