Java Spring数据和JPA的编译时错误
通过使用Spring数据,我正在开发一个java项目,该项目定义了规则、存储库和服务,以便按照JPA规范执行数据访问 这是我的源代码: DALConfig:Java Spring数据和JPA的编译时错误,java,spring,jpa,spring-data,spring-data-jpa,Java,Spring,Jpa,Spring Data,Spring Data Jpa,通过使用Spring数据,我正在开发一个java项目,该项目定义了规则、存储库和服务,以便按照JPA规范执行数据访问 这是我的源代码: DALConfig: @Configuration @ComponentScan(basePackages = { "my.project.dal" }) @PropertySource("classpath:dbconnection.properties") @EnableJpaRepositories("my.project.dal.repository")
@Configuration
@ComponentScan(basePackages = { "my.project.dal" })
@PropertySource("classpath:dbconnection.properties")
@EnableJpaRepositories("my.project.dal.repository")
@EnableTransactionManagement
public class DALConfig {
private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver_class";
private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
private static final String PROPERTY_NAME_POOL_INITIAL_SIZE = "pool.initialsize";
private static final String PROPERTY_NAME_POOL_MAX_IDLE = "pool.maxidle";
private static final String PROPERTY_NAME_DAL_CLASSES_PACKAGE = "entities.packages_to_scan";
private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.showsql";
private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
@Autowired
private Environment environment;
/**
* Method which handles the <i>dataSource</i> bean creation.
* @return Returns a {@link DataSource} class instance which connects to the PRISMA platform DB.
*/
@Bean
public DataSource dataSource() {
Properties props = new Properties();
props.put("driverClassName", environment.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
props.put("url", environment.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
props.put("username", environment.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
props.put("password", environment.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
props.put("initialSize", environment.getRequiredProperty(PROPERTY_NAME_POOL_INITIAL_SIZE));
props.put("maxIdle", environment.getRequiredProperty(PROPERTY_NAME_POOL_MAX_IDLE));
BasicDataSource bds = null;
try {
bds = BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bds;
}
/**
* Method which handles the <i>persistenceExceptionTranslationPostProcessor</i> bean creation.
* @return Returns a {@link PersistenceExceptionTranslationPostProcessor} class instance.
*/
@Bean
public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
return new PersistenceExceptionTranslationPostProcessor();
}
/**
* Method which handles the <i>hibernateExceptionTranslator</i> bean creation.
* @return Returns a {@link HibernateExceptionTranslator} class instance which manages the Hibernate Exceltions translation into Spring's ones.
*/
@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator(){
return new HibernateExceptionTranslator();
}
/**
* Method which handles the <i>transactionManager</i> bean creation.
* @return Returns a {@link PlatformTransactionManager} class instance.
* @throws ClassNotFoundException
*/
@Bean
public PlatformTransactionManager transactionManager() throws ClassNotFoundException {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
/**
* Method which handles the <i>entityManagerFactory</i> bean creation.
* @return Returns a {@link LocalContainerEntityManagerFactoryBean} class instance.
* @throws ClassNotFoundException
*/
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws ClassNotFoundException {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPackagesToScan(environment.getRequiredProperty(PROPERTY_NAME_DAL_CLASSES_PACKAGE));
entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
Properties jpaProperties = new Properties();
jpaProperties.put(PROPERTY_NAME_HIBERNATE_DIALECT, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
}
@Repository
public interface IUserRepository extends CrudRepository<User, String> { }
@Service
public class UserService {
@Autowired
private IUserRepository repository;
@PersistenceContext
EntityManager em;
/**
* Method which finds a {@link User} by the provided <i>username</i>.
*
* @param username
* The username of the {@link User} to be found.
* @return Returns the found {@link User}.
* @throws DataRetrievalFailureException
* If the User doesn't exist in the DB.
* @throws IllegalArgumentException
* If the <i>username</i> is <i>null</i>
*/
public User find(String username) throws DataRetrievalFailureException,
IllegalArgumentException {
User user = repository.findOne(username);
if (user == null)
throw new DataRetrievalFailureException("User with username = \""
+ username + "\" not found");
return user;
}
/**
* Method which inserts the provided {@link User}.
*
* @param user
* The {@link User} to be inserted.
* @return Returns the inserted {@link User}. Use the returned instance for
* further operations as the insert operation might have changed the
* entity instance completely.
* @throws DuplicateKeyException
* If the <i>user</i> already exists in the DB.
* @throws IllegalArgumentException
* If the username of the <i>user</i> is <i>null</i>
* @throws JpaSystemException
* If a unique field is already in the DB.
*/
public User insert(User user) throws DuplicateKeyException,
JpaSystemException {
if (repository.exists(user.getUsername()))
throw new DuplicateKeyException("User with username = \""
+ user.getUsername() + "\" already exists");
return repository.save(user);
}
/**
* Method which deletes the {@link User} identifiedy by <i>username</i>.
*
* @param username
* The username of the {@link User} to be deleted.
* @throws EmptyResultDataAccessException
* If a {@link User} with the provided <i>username</i> doesn't
* exist in the DB.
* @throws IllegalArgumentException
* If the <i>username</i> is <i>null</i>
*/
public void delete(String username) throws EmptyResultDataAccessException,
IllegalArgumentException {
repository.delete(username);
}
/**
* Method which updates the provided {@link User} <i>user</i> in the DB.
*
* @param user
* The updated {@link User}.
* @return Returns the updated {@link User}. Use the returned instance for
* further operations as the insert operation might have changed the
* entity instance completely.
* @throws DataRetrievalFailureException
* If the <i>user</i> doesn't exist in the DB.
* @throws IllegalArgumentException
* If the username of the <i>user</i> is <i>null</i>
*/
public User update(User user) throws DataRetrievalFailureException {
if (!repository.exists(user.getUsername()))
throw new DataRetrievalFailureException("User with username = \""
+ user.getUsername() + "\" not found");
return repository.save(user);
}
}
@配置
@ComponentScan(basePackages={“my.project.dal”})
@PropertySource(“classpath:dbconnection.properties”)
@EnableJpaRepositories(“my.project.dal.repository”)
@启用事务管理
公共类DALConfig{
私有静态最终字符串属性\u NAME\u DATABASE\u DRIVER=“db.DRIVER\u class”;
私有静态最终字符串属性\u NAME\u DATABASE\u PASSWORD=“db.PASSWORD”;
私有静态最终字符串属性\u NAME\u DATABASE\u URL=“db.URL”;
私有静态最终字符串属性\u NAME\u DATABASE\u USERNAME=“db.USERNAME”;
私有静态最终字符串属性\u NAME\u POOL\u INITIAL\u SIZE=“POOL.initialsize”;
私有静态最终字符串属性\u NAME\u POOL\u MAX\u IDLE=“POOL.maxidle”;
私有静态最终字符串属性\u NAME\u DAL\u CLASSES\u PACKAGE=“entities.packages\u to\u scan”;
私有静态最终字符串属性\u NAME\u HIBERNATE\u dialogue=“HIBERNATE.dialogue”;
私有静态最终字符串属性\u NAME\u HIBERNATE\u SHOW\u SQL=“HIBERNATE.showsql”;
私有静态最终字符串属性\u NAME\u HIBERNATE\u FORMAT\u SQL=“HIBERNATE.FORMAT\u SQL”;
@自动连线
私人环境;
/**
*方法,该方法处理数据源bean的创建。
*@return返回连接到PRISMA平台数据库的{@link DataSource}类实例。
*/
@豆子
公共数据源数据源(){
Properties props=新属性();
put(“driverClassName”,environment.getRequiredProperty(PROPERTY\u NAME\u DATABASE\u DRIVER));
props.put(“url”,environment.getRequiredProperty(PROPERTY\u NAME\u DATABASE\u url));
props.put(“username”,environment.getRequiredProperty(PROPERTY\u NAME\u DATABASE\u username));
props.put(“password”,environment.getRequiredProperty(PROPERTY\u NAME\u DATABASE\u password));
props.put(“initialSize”,environment.getRequiredProperty(PROPERTY\u NAME\u POOL\u INITIAL\u SIZE));
props.put(“maxIdle”,environment.getRequiredProperty(PROPERTY\u NAME\u POOL\u MAX\u IDLE));
BasicDataSource bds=null;
试一试{
bds=BasicDataSourceFactory.createDataSource(props);
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回bds;
}
/**
*方法,该方法处理persistenceExceptionTranslationPostProcessor bean创建。
*@return返回{@link PersistenceExceptionTranslationPostProcessor}类实例。
*/
@豆子
公共PersistenceExceptionTranslationPostProcessor PersistenceExceptionTranslationPostProcessor(){
返回新的PersistenceExceptionTranslationPostProcessor();
}
/**
*方法,该方法处理hibernateExceptionTranslator bean的创建。
*@return返回一个{@link HibernateExceptionTranslator}类实例,该实例管理将Hibernate卓越转换为Spring卓越的转换。
*/
@豆子
公共HibernateExceptionTranslator HibernateExceptionTranslator(){
返回新的HibernateeExceptionTranslator();
}
/**
*方法,该方法处理transactionManager bean的创建。
*@return返回一个{@link PlatformTransactionManager}类实例。
*@ClassNotFoundException
*/
@豆子
公共平台transactionManager transactionManager()引发ClassNotFoundException{
JpaTransactionManager transactionManager=新的JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
返回事务管理器;
}
/**
*方法,该方法处理entityManagerFactory bean的创建。
*@return返回一个{@link LocalContainerEntityManagerFactoryBean}类实例。
*@ClassNotFoundException
*/
@豆子
public LocalContainerEntityManagerFactoryBean entityManagerFactory()引发ClassNotFoundException{
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean=新的LocalContainerEntityManagerFactoryBean();
setDataSource(dataSource());
entityManagerFactoryBean.setPackagesToScan(环境.getRequiredProperty(属性\名称\类\包));
entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
Properties JPAPProperties=新属性();
jpaProperties.put(PROPERTY_NAME_HIBERNATE_方言,environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_方言));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL,environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL,environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
entityManagerFactoryBean.setJPapProperties(JPapProperties);
返回entityManagerFactoryBean;
}
}
IUserRepository:
@Configuration
@ComponentScan(basePackages = { "my.project.dal" })
@PropertySource("classpath:dbconnection.properties")
@EnableJpaRepositories("my.project.dal.repository")
@EnableTransactionManagement
public class DALConfig {
private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver_class";
private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
private static final String PROPERTY_NAME_POOL_INITIAL_SIZE = "pool.initialsize";
private static final String PROPERTY_NAME_POOL_MAX_IDLE = "pool.maxidle";
private static final String PROPERTY_NAME_DAL_CLASSES_PACKAGE = "entities.packages_to_scan";
private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.showsql";
private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
@Autowired
private Environment environment;
/**
* Method which handles the <i>dataSource</i> bean creation.
* @return Returns a {@link DataSource} class instance which connects to the PRISMA platform DB.
*/
@Bean
public DataSource dataSource() {
Properties props = new Properties();
props.put("driverClassName", environment.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
props.put("url", environment.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
props.put("username", environment.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
props.put("password", environment.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
props.put("initialSize", environment.getRequiredProperty(PROPERTY_NAME_POOL_INITIAL_SIZE));
props.put("maxIdle", environment.getRequiredProperty(PROPERTY_NAME_POOL_MAX_IDLE));
BasicDataSource bds = null;
try {
bds = BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bds;
}
/**
* Method which handles the <i>persistenceExceptionTranslationPostProcessor</i> bean creation.
* @return Returns a {@link PersistenceExceptionTranslationPostProcessor} class instance.
*/
@Bean
public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
return new PersistenceExceptionTranslationPostProcessor();
}
/**
* Method which handles the <i>hibernateExceptionTranslator</i> bean creation.
* @return Returns a {@link HibernateExceptionTranslator} class instance which manages the Hibernate Exceltions translation into Spring's ones.
*/
@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator(){
return new HibernateExceptionTranslator();
}
/**
* Method which handles the <i>transactionManager</i> bean creation.
* @return Returns a {@link PlatformTransactionManager} class instance.
* @throws ClassNotFoundException
*/
@Bean
public PlatformTransactionManager transactionManager() throws ClassNotFoundException {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
/**
* Method which handles the <i>entityManagerFactory</i> bean creation.
* @return Returns a {@link LocalContainerEntityManagerFactoryBean} class instance.
* @throws ClassNotFoundException
*/
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws ClassNotFoundException {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPackagesToScan(environment.getRequiredProperty(PROPERTY_NAME_DAL_CLASSES_PACKAGE));
entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
Properties jpaProperties = new Properties();
jpaProperties.put(PROPERTY_NAME_HIBERNATE_DIALECT, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
}
@Repository
public interface IUserRepository extends CrudRepository<User, String> { }
@Service
public class UserService {
@Autowired
private IUserRepository repository;
@PersistenceContext
EntityManager em;
/**
* Method which finds a {@link User} by the provided <i>username</i>.
*
* @param username
* The username of the {@link User} to be found.
* @return Returns the found {@link User}.
* @throws DataRetrievalFailureException
* If the User doesn't exist in the DB.
* @throws IllegalArgumentException
* If the <i>username</i> is <i>null</i>
*/
public User find(String username) throws DataRetrievalFailureException,
IllegalArgumentException {
User user = repository.findOne(username);
if (user == null)
throw new DataRetrievalFailureException("User with username = \""
+ username + "\" not found");
return user;
}
/**
* Method which inserts the provided {@link User}.
*
* @param user
* The {@link User} to be inserted.
* @return Returns the inserted {@link User}. Use the returned instance for
* further operations as the insert operation might have changed the
* entity instance completely.
* @throws DuplicateKeyException
* If the <i>user</i> already exists in the DB.
* @throws IllegalArgumentException
* If the username of the <i>user</i> is <i>null</i>
* @throws JpaSystemException
* If a unique field is already in the DB.
*/
public User insert(User user) throws DuplicateKeyException,
JpaSystemException {
if (repository.exists(user.getUsername()))
throw new DuplicateKeyException("User with username = \""
+ user.getUsername() + "\" already exists");
return repository.save(user);
}
/**
* Method which deletes the {@link User} identifiedy by <i>username</i>.
*
* @param username
* The username of the {@link User} to be deleted.
* @throws EmptyResultDataAccessException
* If a {@link User} with the provided <i>username</i> doesn't
* exist in the DB.
* @throws IllegalArgumentException
* If the <i>username</i> is <i>null</i>
*/
public void delete(String username) throws EmptyResultDataAccessException,
IllegalArgumentException {
repository.delete(username);
}
/**
* Method which updates the provided {@link User} <i>user</i> in the DB.
*
* @param user
* The updated {@link User}.
* @return Returns the updated {@link User}. Use the returned instance for
* further operations as the insert operation might have changed the
* entity instance completely.
* @throws DataRetrievalFailureException
* If the <i>user</i> doesn't exist in the DB.
* @throws IllegalArgumentException
* If the username of the <i>user</i> is <i>null</i>
*/
public User update(User user) throws DataRetrievalFailureException {
if (!repository.exists(user.getUsername()))
throw new DataRetrievalFailureException("User with username = \""
+ user.getUsername() + "\" not found");
return repository.save(user);
}
}
@存储库
公共接口IUserRepository扩展了Crudepository{}
用户服务:
@Configuration
@ComponentScan(basePackages = { "my.project.dal" })
@PropertySource("classpath:dbconnection.properties")
@EnableJpaRepositories("my.project.dal.repository")
@EnableTransactionManagement
public class DALConfig {
private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver_class";
private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
private static final String PROPERTY_NAME_POOL_INITIAL_SIZE = "pool.initialsize";
private static final String PROPERTY_NAME_POOL_MAX_IDLE = "pool.maxidle";
private static final String PROPERTY_NAME_DAL_CLASSES_PACKAGE = "entities.packages_to_scan";
private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.showsql";
private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
@Autowired
private Environment environment;
/**
* Method which handles the <i>dataSource</i> bean creation.
* @return Returns a {@link DataSource} class instance which connects to the PRISMA platform DB.
*/
@Bean
public DataSource dataSource() {
Properties props = new Properties();
props.put("driverClassName", environment.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
props.put("url", environment.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
props.put("username", environment.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
props.put("password", environment.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
props.put("initialSize", environment.getRequiredProperty(PROPERTY_NAME_POOL_INITIAL_SIZE));
props.put("maxIdle", environment.getRequiredProperty(PROPERTY_NAME_POOL_MAX_IDLE));
BasicDataSource bds = null;
try {
bds = BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bds;
}
/**
* Method which handles the <i>persistenceExceptionTranslationPostProcessor</i> bean creation.
* @return Returns a {@link PersistenceExceptionTranslationPostProcessor} class instance.
*/
@Bean
public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
return new PersistenceExceptionTranslationPostProcessor();
}
/**
* Method which handles the <i>hibernateExceptionTranslator</i> bean creation.
* @return Returns a {@link HibernateExceptionTranslator} class instance which manages the Hibernate Exceltions translation into Spring's ones.
*/
@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator(){
return new HibernateExceptionTranslator();
}
/**
* Method which handles the <i>transactionManager</i> bean creation.
* @return Returns a {@link PlatformTransactionManager} class instance.
* @throws ClassNotFoundException
*/
@Bean
public PlatformTransactionManager transactionManager() throws ClassNotFoundException {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
/**
* Method which handles the <i>entityManagerFactory</i> bean creation.
* @return Returns a {@link LocalContainerEntityManagerFactoryBean} class instance.
* @throws ClassNotFoundException
*/
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws ClassNotFoundException {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPackagesToScan(environment.getRequiredProperty(PROPERTY_NAME_DAL_CLASSES_PACKAGE));
entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
Properties jpaProperties = new Properties();
jpaProperties.put(PROPERTY_NAME_HIBERNATE_DIALECT, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
}
@Repository
public interface IUserRepository extends CrudRepository<User, String> { }
@Service
public class UserService {
@Autowired
private IUserRepository repository;
@PersistenceContext
EntityManager em;
/**
* Method which finds a {@link User} by the provided <i>username</i>.
*
* @param username
* The username of the {@link User} to be found.
* @return Returns the found {@link User}.
* @throws DataRetrievalFailureException
* If the User doesn't exist in the DB.
* @throws IllegalArgumentException
* If the <i>username</i> is <i>null</i>
*/
public User find(String username) throws DataRetrievalFailureException,
IllegalArgumentException {
User user = repository.findOne(username);
if (user == null)
throw new DataRetrievalFailureException("User with username = \""
+ username + "\" not found");
return user;
}
/**
* Method which inserts the provided {@link User}.
*
* @param user
* The {@link User} to be inserted.
* @return Returns the inserted {@link User}. Use the returned instance for
* further operations as the insert operation might have changed the
* entity instance completely.
* @throws DuplicateKeyException
* If the <i>user</i> already exists in the DB.
* @throws IllegalArgumentException
* If the username of the <i>user</i> is <i>null</i>
* @throws JpaSystemException
* If a unique field is already in the DB.
*/
public User insert(User user) throws DuplicateKeyException,
JpaSystemException {
if (repository.exists(user.getUsername()))
throw new DuplicateKeyException("User with username = \""
+ user.getUsername() + "\" already exists");
return repository.save(user);
}
/**
* Method which deletes the {@link User} identifiedy by <i>username</i>.
*
* @param username
* The username of the {@link User} to be deleted.
* @throws EmptyResultDataAccessException
* If a {@link User} with the provided <i>username</i> doesn't
* exist in the DB.
* @throws IllegalArgumentException
* If the <i>username</i> is <i>null</i>
*/
public void delete(String username) throws EmptyResultDataAccessException,
IllegalArgumentException {
repository.delete(username);
}
/**
* Method which updates the provided {@link User} <i>user</i> in the DB.
*
* @param user
* The updated {@link User}.
* @return Returns the updated {@link User}. Use the returned instance for
* further operations as the insert operation might have changed the
* entity instance completely.
* @throws DataRetrievalFailureException
* If the <i>user</i> doesn't exist in the DB.
* @throws IllegalArgumentException
* If the username of the <i>user</i> is <i>null</i>
*/
public User update(User user) throws DataRetrievalFailureException {
if (!repository.exists(user.getUsername()))
throw new DataRetrievalFailureException("User with username = \""
+ user.getUsername() + "\" not found");
return repository.save(user);
}
}
@服务
公共类用户服务{
@自动连线
私有IUSER存储库;
@持久上下文
实体管理器;
/**
*方法,该方法通过提供的用户名查找{@link User}。
*
*@param用户名
*要找到的{@link User}的用户名。
*@return返回找到的{@link User}。
*@抛出DataRetrievalFailureException
*如果用户在数据库中不存在。
*@galargumentException
*如果用户名为空
*/
公共用户查找(字符串用户名)引发DataRetrievalFailureException,
非法辩论例外{
User=repository.findOne(用户名);
如果(用户)==