abtin021
چهارشنبه 10 اردیبهشت 1393, 02:10 صبح
سلام من میخوام یک resource که در application server استفاده کردم رو به وسیله spring بخونم یعنی بدون EJB و غیر از اون روشی که initialcontext می کنی ولی موقعی که lookup میکنم Entity manager خالیه null
این فایل Persistence.xml هستش که resource jdbc/victor رو میشناسه
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="JPA" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/victor</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
این اون فایل config برای Spring هستش
<?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:p="http://www.springframework.org/schema/p"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
<jee:jndi-lookup id="prawalewa" jndi-name="jdbc/victor"/>
<!-- In order to enable EntityManager injection -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnn otationBeanPostProcessor">
<property name="persistenceUnits">
<map>
<entry key="JPA" value="persistence/JPA" />
</map>
</property>
</bean>
</beans>
اینم Entity
package entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author Administrator
*/
@Entity
@Table(name="student")
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String level;
private String name;
public Student() {
}
public Student(String level, String name) {
this.level = level;
this.name = name;
}
public Student(Long id, String level, String name) {
this.id = id;
this.level = level;
this.name = name;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
اینم اون کلاسی که Lookup میکنه
package manager;
import entity.Student;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import org.springframework.stereotype.Repository;
@Repository
public class JndiManager {
@PersistenceContext(unitName = "JPA")
private EntityManager em;
@Transactional
public void save(){
Student st=new Student("TEST", "TESTTTTT");
em.persist(st);
}
}
من یه خوندم که در فایل web.xml باید persistence unit رو تعریف کنی منم همین کارو انجام دادم
<persistence-unit-ref>
<persistence-unit-ref-name>persistence/JPA</persistence-unit-ref-name>
<persistence-unit-name>JPA</persistence-unit-name>
</persistence-unit-ref>
لطفاً بگید مشکل از کجاست.... :متفکر:
ممنون:قلب:
این فایل Persistence.xml هستش که resource jdbc/victor رو میشناسه
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="JPA" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/victor</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
این اون فایل config برای Spring هستش
<?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:p="http://www.springframework.org/schema/p"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
<jee:jndi-lookup id="prawalewa" jndi-name="jdbc/victor"/>
<!-- In order to enable EntityManager injection -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnn otationBeanPostProcessor">
<property name="persistenceUnits">
<map>
<entry key="JPA" value="persistence/JPA" />
</map>
</property>
</bean>
</beans>
اینم Entity
package entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author Administrator
*/
@Entity
@Table(name="student")
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String level;
private String name;
public Student() {
}
public Student(String level, String name) {
this.level = level;
this.name = name;
}
public Student(Long id, String level, String name) {
this.id = id;
this.level = level;
this.name = name;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
اینم اون کلاسی که Lookup میکنه
package manager;
import entity.Student;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import org.springframework.stereotype.Repository;
@Repository
public class JndiManager {
@PersistenceContext(unitName = "JPA")
private EntityManager em;
@Transactional
public void save(){
Student st=new Student("TEST", "TESTTTTT");
em.persist(st);
}
}
من یه خوندم که در فایل web.xml باید persistence unit رو تعریف کنی منم همین کارو انجام دادم
<persistence-unit-ref>
<persistence-unit-ref-name>persistence/JPA</persistence-unit-ref-name>
<persistence-unit-name>JPA</persistence-unit-name>
</persistence-unit-ref>
لطفاً بگید مشکل از کجاست.... :متفکر:
ممنون:قلب: