Last active
August 29, 2015 14:12
-
-
Save an-sangkil/05c23d47c36db6b0aa4c to your computer and use it in GitHub Desktop.
Hibernate JAVA Configration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.skan.potal.hibernate.controller; | |
import java.util.List; | |
import javax.annotation.Resource; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.validation.Valid; | |
import org.springframework.data.domain.Page; | |
import org.springframework.data.domain.PageRequest; | |
import org.springframework.data.domain.Sort.Direction; | |
import org.springframework.http.HttpRequest; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.ui.ModelMap; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.ResponseBody; | |
import com.skan.potal.hibernate.model.User; | |
import com.skan.potal.hibernate.service.UserService; | |
/** | |
* <pre> | |
* Class Name : HibernateTestController.java | |
* Description : | |
* Modification Information | |
* | |
* 수정일 수정자 수정내용 | |
* ──────────── ───────── ─────────────────────────────── | |
* 2015. 1. 5. ask 최초생성 | |
* </pre> | |
* | |
* @author ask | |
* @since 2015. 1. 5. | |
* @version | |
* | |
* Copyright (C) 2014 by SKAN.COMPANY All right reserved. | |
*/ | |
@Controller | |
public class HibernateTestController { | |
@Resource | |
private UserService userService; | |
@RequestMapping(value="htest01") | |
public void hTestSave(@Valid User user, HttpServletRequest request, HttpServletResponse response) throws Exception { | |
user.setUsername("ansangkil"); | |
user.setName("haha~~"); | |
this.userService.insertUser(user); | |
} | |
@RequestMapping(value="htest02") | |
public @ResponseBody List<User> hTestFindAll(HttpServletRequest request, HttpServletResponse response) throws Exception { | |
List<User> user = userService.findAllUsers(); | |
for (User o : user) { | |
System.out.println(o); | |
} | |
return user; | |
} | |
@RequestMapping(value="htest03") | |
public | |
//@ResponseBody | |
String hTestPagination(HttpServletRequest request, HttpServletResponse response , ModelMap modeMap) throws Exception { | |
Page<User> userPaging = userService.findUser(new PageRequest(1, 10, Direction.ASC , "id")); | |
modeMap.put("userPaging", userPaging); | |
return "/test/paging/pagingTest"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.skan.potal.config; | |
import java.util.Properties; | |
import javax.persistence.EntityManagerFactory; | |
import javax.sql.DataSource; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.core.env.Environment; | |
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; | |
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | |
import org.springframework.jdbc.datasource.DriverManagerDataSource; | |
import org.springframework.orm.jpa.JpaTransactionManager; | |
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | |
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; | |
import org.springframework.transaction.PlatformTransactionManager; | |
import org.springframework.transaction.annotation.EnableTransactionManagement; | |
/** | |
* <pre> | |
* Class Name : JPAConfig.java | |
* Description : | |
* Modification Information | |
* | |
* 수정일 수정자 수정내용 | |
* ──────────── ───────── ─────────────────────────────── | |
* 2014. 12. 9. ask 최초생성 | |
* </pre> | |
* | |
* @author ask | |
* @since 2014. 12. 9. | |
* @version | |
* | |
* Copyright (C) 2014 by SKAN.COMPANY All right reserved. | |
*/ | |
@Configuration | |
@EnableTransactionManagement | |
//@PropertySource({ "classpath:persistence-mysql.properties", "classpath:persistence-postgresql.properties",}) | |
@ComponentScan({ "com.skan.potal.hibernate" }) | |
@EnableJpaRepositories(basePackages="com.skan.potal.hibernate.dao") | |
public class PersistenceJPAConfig { | |
@Autowired private Environment env; | |
//@Autowired private DataSourceConfig dataSourceConfig; | |
public PersistenceJPAConfig() { | |
super(); | |
} | |
@Bean | |
public LocalContainerEntityManagerFactoryBean entityManagerFactory() { | |
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); | |
em.setDataSource(dataSource()); | |
em.setPackagesToScan(new String[]{"com.skan.potal.hibernate.model"}); | |
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); | |
em.setJpaVendorAdapter(vendorAdapter); | |
em.setJpaProperties(additionalProperties()); | |
return em; | |
} | |
@Bean | |
public DataSource dataSource (){ | |
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource(); | |
driverManagerDataSource.setDriverClassName("org.postgresql.Driver"); | |
driverManagerDataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/potaldb"); | |
driverManagerDataSource.setUsername("skan"); | |
driverManagerDataSource.setPassword("1111"); | |
return driverManagerDataSource; | |
} | |
@Bean | |
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) { | |
final JpaTransactionManager transactionManager = new JpaTransactionManager(); | |
transactionManager.setEntityManagerFactory(emf); | |
return transactionManager; | |
} | |
@Bean | |
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { | |
return new PersistenceExceptionTranslationPostProcessor(); | |
} | |
final Properties additionalProperties() { | |
final Properties hibernateProperties = new Properties(); | |
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "update");//drop-and-create-tables | |
hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgresPlusDialect"); | |
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true"); | |
return hibernateProperties; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.skan.potal.hibernate.model; | |
import java.io.Serializable; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.GenerationType; | |
import javax.persistence.Id; | |
import javax.persistence.Table; | |
import org.springframework.data.jpa.domain.AbstractPersistable; | |
/** | |
* <pre> | |
* Class Name : UserEntity.java | |
* Description : | |
* Modification Information | |
* | |
* 수정일 수정자 수정내용 | |
* ──────────── ───────── ─────────────────────────────── | |
* 2014. 12. 4. ask 최초생성 | |
* </pre> | |
* | |
* @author ask | |
* @since 2014. 12. 4. | |
* @version | |
* | |
* Copyright (C) 2014 by SKAN.COMPANY All right reserved. | |
*/ | |
@Entity | |
@Table(name="user1") | |
public class User extends AbstractPersistable<Long> implements Serializable{ | |
/** | |
* | |
*/ | |
private static final long serialVersionUID = 1L; | |
@Column(name = "username", nullable = false) | |
private String username; | |
@Column(name = "name", nullable = true) | |
private String name; | |
public String getUsername() { | |
return username; | |
} | |
public void setUsername(String username) { | |
this.username = username; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
/* (non-Javadoc) | |
* @see java.lang.Object#toString() | |
*/ | |
@Override | |
public String toString() { | |
return "User [username=" + username + ", name=" + name + "]"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.skan.potal.hibernate.dao; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
import com.skan.potal.hibernate.model.User; | |
/** | |
* <pre> | |
* Class Name : UserDao.java | |
* Description : | |
* Modification Information | |
* | |
* 수정일 수정자 수정내용 | |
* ──────────── ───────── ─────────────────────────────── | |
* 2014. 12. 4. ask 최초생성 | |
* </pre> | |
* | |
* @author ask | |
* @since 2014. 12. 4. | |
* @version | |
* | |
* Copyright (C) 2014 by SKAN.COMPANY All right reserved. | |
*/ | |
public interface UserDAO extends JpaRepository<User, Long> { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.skan.potal.hibernate.service; | |
import java.util.List; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.data.domain.Page; | |
import org.springframework.data.domain.PageRequest; | |
import org.springframework.stereotype.Service; | |
import com.skan.potal.hibernate.dao.UserDAO; | |
import com.skan.potal.hibernate.model.User; | |
/** | |
* <pre> | |
* Class Name : UserService.java | |
* Description : | |
* Modification Information | |
* | |
* 수정일 수정자 수정내용 | |
* ──────────── ───────── ─────────────────────────────── | |
* 2014. 12. 4. ask 최초생성 | |
* </pre> | |
* | |
* @author ask | |
* @since 2014. 12. 4. | |
* @version | |
* | |
* Copyright (C) 2014 by SKAN.COMPANY All right reserved. | |
*/ | |
@Service("userService") | |
public class UserServiceImpl implements UserService { | |
@Autowired | |
UserDAO userDAO; | |
public void insertUser(User user) { | |
userDAO.save(user); | |
} | |
public List<User> findAllUsers() { | |
return userDAO.findAll(); | |
} | |
@Override | |
public Page<User> findUser(PageRequest pageRequest) { | |
return userDAO.findAll(pageRequest); | |
} | |
@Override | |
public User findUser(Long pk) { | |
return userDAO.findOne(pk); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
하이버네이트 JPA 테스트코드