Skip to content

Instantly share code, notes, and snippets.

View jeetprksh's full-sized avatar
💭
Silly side projects

Jeet Prakash jeetprksh

💭
Silly side projects
View GitHub Profile
public class UserDaoImpl extends CommonDaoImpl implements UserDAO {
@Override
public int createUser(User user) throws HibernateException {
return super.saveEntity(user);
}
}
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String firstName;
private String lastName;
public class Tests {
private static final Logger logger = Logger.getLogger(Tests.class.getName());
private UserDAO userDao;
public Tests() {
this.userDao = new UserDaoImpl();
}
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-5.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testDB</property>
<property name="hibernate.connection.username">root</property>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jeetprksh</groupId>
<artifactId>hibernate-examples</artifactId>
<version>1.0-SNAPSHOT</version>
<name>hibernate-examples</name>
create table if not exists `users` (
`id` int not null auto_increment,
`firstName` varchar(20),
`lastName` varchar(20),
`age` int,
primary key (`id`)
);
insert into `users` values (1, "john", "doe", 29);
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
@Transactional(readOnly = true)
public ServerResponse getUser(Long userId) {
ServerResponse response = new ServerResponse();
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public UserDTO getUser(Long userId) {
TypedQuery<UserDTO> typedQuery = sessionFactory.getCurrentSession().createQuery("from UserDTO where id=" + userId.toString());
return typedQuery.getSingleResult();
@RestController()
@RequestMapping("user")
public class UserController {
@Autowired
private UserService service;
@RequestMapping(value = "{userId}", method = RequestMethod.GET)
public @ResponseBody ServerResponse getUser(@PathVariable("userId") Long userId) {
return this.service.getUser(userId);
@Configuration
@EnableWebMvc
@ComponentScan("com.spring5.app")
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(customJackson2HttpMessageConverter());
super.addDefaultHttpMessageConverters(converters);
}