Use Spring ORM to manage the persistence object
Use Spring ORM to manage the persistence object
Spring ORM is part of the Spring framework, which provides a simple and flexible way to manage the persistent objects in the application.By using Spring ORM, developers can map objects to the relationship database or other persistence storage to achieve durable and access of data.
One of the main features of Spring ORM is that it integrates with multiple persistence technology, including Hibernate, JPA, and Mybatis.This means that developers can choose the lasting technologies that are most suitable for their needs, and can easily switch between different technologies without having to modify other parts of the application.
Below is an example of Java code that uses Spring ORM for persistence objects:
First of all, you need to configure the data source and sessionFactory in the Spring configuration file to create a database connection and manage the Hibernate session.
@Configuration
@EnableTransactionManagement
public class HibernateConfig {
@Bean
public DataSource dataSource() {
// Configure the data source
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan("com.example.model");
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
private Properties hibernateProperties() {
// Configure the hibernate attribute
}
}
Next, you need to create persistent objects and corresponding data access interfaces.Suppose there is a physical class called `user` and a data access interface corresponding to` user` userRepository`.
public class User {
private Long id;
private String username;
private String password;
// Eliminate the constructor, Getter, and Setter method
}
public interface UserRepository {
void save(User user);
User findById(Long id);
void delete(User user);
}
Then, implement the `UserRePOSITORY` interface and use the spring`@repository` annotation to mark it for automatic injection when needed.
@Repository
public class UserRepositoryImpl implements UserRepository {
@Autowired
private SessionFactory sessionFactory;
@Override
public void save(User user) {
sessionFactory.getCurrentSession().save(user);
}
@Override
public User findById(Long id) {
return sessionFactory.getCurrentSession().get(User.class, id);
}
@Override
public void delete(User user) {
sessionFactory.getCurrentSession().delete(user);
}
}
Finally, where you need to use a durable object, you can access and manage objects by declare the instance of the `UserRePOSITOSITOSITOSITOSITOSITOSITORY` interface.
@Service
@Transactional
public class UserService {
@Autowired
private UserRepository userRepository;
public void createUser(User user) {
userRepository.save(user);
}
public User getUserById(Long id) {
return userRepository.findById(id);
}
public void deleteUser(User user) {
userRepository.delete(user);
}
}
By using Spring ORM for the management of persistent objects, developers can simplify the writing and maintenance of data access code, and achieve flexible persistent technology integration.Regardless of the selection of Hibernate, JPA, or Mybatis, Spring ORM provides consistent programming models and transaction management functions, enabling developers to focus more on the realization of business logic.