[SPRING] 봄 + JPA + 최대 절전 모드
SPRING봄 + JPA + 최대 절전 모드
나는 봄에 초보자입니다. 스프링 3.1.3 및 JPA 2.0으로 간단한 웹 응용 프로그램을 설치하려고합니다. 필요한 모든 libs를 WEB-INF / lib에 추가했습니다. 시작할 때 오류는 없지만 DaoImpl 파일의 entityManager는 null입니다. 그래서, 이것은 나의 구성입니다 :
이 persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="fcmsServer" transaction-type="RESOURCE_LOCAL">
</persistence-unit>
</persistence>
fcms-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- the application context definition for the fcmsServer DispatcherServlet -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="fcmsServer" />
<property name="dataSource" ref="dataSource" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" />
</bean>
</property>
<property name="persistenceUnitManager">
<bean
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="defaultDataSource" ref="dataSource" />
</bean>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:tcp://localhost/~/fcms" />
<property name="username" value="sa" />
<property name="password" value="sa" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="persistenceAnnotation"
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="userDao" class="fcms.data.user.UserDAOImpl">
</bean>
<bean name="/user.htm" class="fcms.controller.UserController">
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
DaoImpl 클래스 :
@Repository
public class UserDAOImpl implements UserDAO {
@PersistenceContext(unitName = "fcmsServer")
private EntityManager entityManager;
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public User getUserById(long id) {
return entityManager.find(User.class,id);
}
@Override
public void addUser(User u) {
entityManager.persist(u);
}
}
사용자 클래스
@Entity
@Table(name = "User")
public class User implements Serializable {
@Id @GeneratedValue
private long id;
@Column(name = "lastName", nullable = false)
private String lastName;
@Column(name = "firstName", nullable = false)
private String firstName;
@Column(name = "birthDate", nullable = true)
private Date birthDate;
private static final long serialVersionUID = 1L;
}
그래서 UserController에서 addUser ()를 호출하려고하면 persist 동안 NullPointerException이 발생합니다.
해결법
-
==============================
1.스프링이 주석을 검색하도록 설정을 잃어 버렸다고 생각합니다 (
요소 사이에 다음을 추가하십시오. 스프링이 주석을 검색하도록 설정을 잃어 버렸다고 생각합니다 (
요소 사이에 다음을 추가하십시오. <context:annotation-config />
그리고 DAO가있는 패키지를 다음과 같이 추가해야 할 수도 있습니다. 그러나 나는 이것이 필요하다고 생각하지 않는다.
<context:component-scan base-package="your.package" />
-
==============================
2.이것은 코드의 작동 조각입니다 :
이것은 코드의 작동 조각입니다 :
Spring.xml :
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="com.package.dao" /> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <property name="persistenceUnitName" value="fcmsServer" /> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> </beans>
DAO 빈 :
@Repository public class UserDaoBean implements UserDao { @PersistenceContext protected EntityManager em; }
Persistence.xml :
<?xml version="1.0" encoding="UTF-8" ?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="fcmsServer" transaction-type="RESOURCE_LOCAL"> <class>com.package.entity.</class> <properties> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/db_name"/> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> <property name="javax.persistence.jdbc.user" value="admin"/> <property name="javax.persistence.jdbc.password" value="admin"/> </properties> </persistence-unit> </persistence>
from https://stackoverflow.com/questions/14855181/spring-jpa-hibernate by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] javax.naming.NameNotFoundException : 이름 [comp / env]가이 Context에 바인드되어 있지 않다 (0) | 2019.05.15 |
---|---|
[SPRING] json을 객체 목록으로 변환 (0) | 2019.05.15 |
[SPRING] Struts2; StrutsSpringTestCase JUnit 테스트를 위해 세션 열기 (0) | 2019.05.15 |
[SPRING] JdbcTemplate - SQL MERGE를 사용하여 Oracle BLOB 삽입 또는 업데이트 (0) | 2019.05.15 |
[SPRING] Spring : 애플리케이션 + 웹 컨텍스트를 어떻게 구성해야합니까? (0) | 2019.05.15 |