JPA:对象不是已知的实体类型。完全不知道如何使用模块化域(持久性)项目配置 Spring 网络应用程序
JPA: Object is not a known Entity type. Completely confused as to how to configure a Spring web app with a modular domain (persistence) project
我正在使用 Spring 框架创建一个 Web 应用程序,并尝试以模块化方式进行。使用 NetBeans,我有 2 个项目:一个包含我的应用程序逻辑,另一个包含我的实际 Web 应用程序。
在我的域应用程序中,我有使用 JPA 的 DAO,并且也在那里进行了测试。现在,当我试图在我的 Spring 模块中重用这些 classes 时,问题就出现了。我对这两个项目都使用 Maven,并在我的 Spring 模块中添加了对我的域项目的依赖。我对如何配置这一切感到很困惑,这是我第一次制作这样的应用程序,事实上,也使用 Spring 和 JPA。同样奇怪的是,当我的电脑重新启动时我第一次启动服务器时,它会工作,但随后重新启动服务器将导致 JPA 层不再工作。
当我试图保留我的对象时出现错误:
Object: com.ucll.lolwithbuddies.domain.League@19f is not a known entity type.
我尝试将 JAR 用于我的持久性模块并将其放入我的 Web 应用程序的 WEB-INF/lib
文件夹中,但随后出现以下错误:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named LoLWithBuddiesPU
persistence.xml 位于 LoLWithBuddies/src/main/resources/META-INF
:
<?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="LoLWithBuddiesPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.ucll.lolwithbuddies.domain.League</class>
<class>com.ucll.lolwithbuddies.domain.Membership</class>
<class>com.ucll.lolwithbuddies.domain.Player</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/LoLWithBuddies;create=true"/>
<property name="javax.persistence.jdbc.user" value="app"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.password" value="app"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
LoLWithBuddies/src/main/java/com/ucll/LoLWithBuddies/db/RelationalLeagueDB:
package com.ucll.lolwithbuddies.db;
import com.ucll.lolwithbuddies.domain.League;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
/**
*
* @author Christophe
*/
public class RelationalLeagueDB implements LeagueDB {
private EntityManagerFactory factory;
private EntityManager manager;
public RelationalLeagueDB(String name) {
factory = Persistence.createEntityManagerFactory(name);
}
@Override
public League get(long id) throws DBException {
try {
manager = factory.createEntityManager();
League league = manager.find(League.class, id);
if (league == null) {
throw new DBException("League not found");
}
return league;
} catch (Exception e) {
throw new DBException(e.getMessage());
} finally {
manager.close();
}
}
@Override
public List<League> getAll() throws DBException {
try {
manager = factory.createEntityManager();
Query query = manager.createQuery("select l from League l");
return query.getResultList();
} catch (Exception e) {
throw new DBException(e.getMessage());
} finally {
manager.close();
}
}
@Override
public void update(League league) throws DBException {
try {
manager = factory.createEntityManager();
//update in memory reflected to database inside transaction
League leagueInDB = manager.find(League.class, league.getId());
manager.getTransaction().begin();
leagueInDB.setName(league.getName());
manager.flush();
manager.getTransaction().commit();
} catch (Exception e) {
throw new DBException(e.getMessage());
} finally {
manager.close();
}
}
@Override
public void delete(long id) throws DBException {
try {
manager = factory.createEntityManager();
League league = manager.find(League.class, id);
manager.getTransaction().begin();
manager.remove(league);
manager.flush();
manager.getTransaction().commit();
} catch (Exception e) {
throw new DBException(e.getMessage());
} finally {
// manager.close();
}
}
@Override
public void add(League item) throws DBException {
try {
manager = factory.createEntityManager();
manager.getTransaction().begin();
manager.persist(item);
manager.flush();
manager.getTransaction().commit();
} catch (Exception e) {
throw new DBException(e.getMessage());
} finally {
manager.close();
}
}
@Override
public void closeConnection() throws DBException {
factory.close();
}
}
pom.xml 持久性项目:
<?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.ucll</groupId>
<artifactId>LoLWithBuddies</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
pom.xml 对于网络项目:
<?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.ucll</groupId>
<artifactId>LoLWithBuddies-web</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>LoLWithBuddies-web</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.ucll</groupId>
<artifactId>LoLWithBuddies</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>4.0.1.RELEASE</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument-tomcat</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.servlet.jsp.jstl</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>unknown-jars-temp-repo</id>
<name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
<url>file:${project.basedir}/lib</url>
</repository>
</repositories>
</project>
示例控制器,我在我的网络应用程序中从我的域项目自动装配我的服务。此服务持有 RelationalLeagueDB class:
LoLWithBuddies-web/src/main/java/com/ucll/LoLWithBuddies/controller/LeagueController.java
package com.ucll.lolwithbuddies.controller;
import com.ucll.lolwithbuddies.domain.League;
import com.ucll.lolwithbuddies.service.Service;
import com.ucll.lolwithbuddies.service.ServiceException;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
*
* @author Christophe
*/
@Controller
@RequestMapping(value="/league")
public class LeagueController {
@Autowired
private Service service;
private boolean update=false;
@RequestMapping(method=RequestMethod.GET)
public ModelAndView getLeagues() throws ServiceException {
List<League> leagues = service.getAllLeagues();
return new ModelAndView("leagues", "leagues", leagues);
}
@RequestMapping(value="/new", method=RequestMethod.GET)
public ModelAndView getNewForm() {
update=false;
League newLeague = new League();
return new ModelAndView("leagueform", "leagueForm", newLeague);
}
@RequestMapping(method=RequestMethod.POST)
public String save(@Valid @ModelAttribute("leagueForm") League leagueForm, BindingResult result) throws ServiceException {
if (result.hasErrors()) {
return "leagueform";
}
if(update) {
service.updateLeague(leagueForm);
} else {
service.addLeague(leagueForm);
}
update=false;
return "redirect:/league";
}
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public ModelAndView getEditForm(@PathVariable long id) throws ServiceException {
update=true;
return new ModelAndView("leagueform", "leagueForm", service.getLeague(id));
}
@RequestMapping(value="/{id}/delete", method=RequestMethod.GET)
public ModelAndView getDeleteConfirmation(@PathVariable long id) throws ServiceException {
return new ModelAndView("leaguedeleteconfirm", "delete", service.getLeague(id));
}
@RequestMapping(value="/delete", method=RequestMethod.POST)
public String delete(@ModelAttribute("delete") League toDelete) throws ServiceException {
service.deleteLeague(toDelete.getId());
return "redirect:/league";
}
}
服务 bean 在基于 Java 的配置中这样配置:
@Bean(name = "service", destroyMethod = "closeConnection")
public Service service() throws ServiceException {
Service bean = new LeagueService("JPA");
return bean;
}
注意我可以将参数传递给此服务(JPA 或本地)。我想将 JPA 用于我的生产代码,但为了测试我可以传递本地参数以使用内存中的 Map 存储库,它工作正常。
谁能给我指出正确的方向?
好的,我找到问题了。正如我所说,当我第一次 运行 应用程序时,应用程序可以正常运行。但是,下次当我第一次尝试重新 运行 我的应用程序时,说因为我做了一些更改并且不得不重建,GlassFish,我的应用程序部署的服务器,不会重新部署我的应用程序,并且旧版本保留在服务器上,旧上下文,因此我的实体是 'old version of themself'。在某种程度上,我的服务 destroyMethod 似乎没有被调用。
当我强制取消部署我的应用程序,重新启动服务器,然后重新运行我的应用程序时,它会工作。但是,目前我必须手动执行此操作。现在正在研究 maven-glassfish-plugin 以自动执行此操作。
我正在使用 Spring 框架创建一个 Web 应用程序,并尝试以模块化方式进行。使用 NetBeans,我有 2 个项目:一个包含我的应用程序逻辑,另一个包含我的实际 Web 应用程序。
在我的域应用程序中,我有使用 JPA 的 DAO,并且也在那里进行了测试。现在,当我试图在我的 Spring 模块中重用这些 classes 时,问题就出现了。我对这两个项目都使用 Maven,并在我的 Spring 模块中添加了对我的域项目的依赖。我对如何配置这一切感到很困惑,这是我第一次制作这样的应用程序,事实上,也使用 Spring 和 JPA。同样奇怪的是,当我的电脑重新启动时我第一次启动服务器时,它会工作,但随后重新启动服务器将导致 JPA 层不再工作。
当我试图保留我的对象时出现错误:
Object: com.ucll.lolwithbuddies.domain.League@19f is not a known entity type.
我尝试将 JAR 用于我的持久性模块并将其放入我的 Web 应用程序的 WEB-INF/lib
文件夹中,但随后出现以下错误:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named LoLWithBuddiesPU
persistence.xml 位于 LoLWithBuddies/src/main/resources/META-INF
:
<?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="LoLWithBuddiesPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.ucll.lolwithbuddies.domain.League</class>
<class>com.ucll.lolwithbuddies.domain.Membership</class>
<class>com.ucll.lolwithbuddies.domain.Player</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/LoLWithBuddies;create=true"/>
<property name="javax.persistence.jdbc.user" value="app"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.password" value="app"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
LoLWithBuddies/src/main/java/com/ucll/LoLWithBuddies/db/RelationalLeagueDB:
package com.ucll.lolwithbuddies.db;
import com.ucll.lolwithbuddies.domain.League;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
/**
*
* @author Christophe
*/
public class RelationalLeagueDB implements LeagueDB {
private EntityManagerFactory factory;
private EntityManager manager;
public RelationalLeagueDB(String name) {
factory = Persistence.createEntityManagerFactory(name);
}
@Override
public League get(long id) throws DBException {
try {
manager = factory.createEntityManager();
League league = manager.find(League.class, id);
if (league == null) {
throw new DBException("League not found");
}
return league;
} catch (Exception e) {
throw new DBException(e.getMessage());
} finally {
manager.close();
}
}
@Override
public List<League> getAll() throws DBException {
try {
manager = factory.createEntityManager();
Query query = manager.createQuery("select l from League l");
return query.getResultList();
} catch (Exception e) {
throw new DBException(e.getMessage());
} finally {
manager.close();
}
}
@Override
public void update(League league) throws DBException {
try {
manager = factory.createEntityManager();
//update in memory reflected to database inside transaction
League leagueInDB = manager.find(League.class, league.getId());
manager.getTransaction().begin();
leagueInDB.setName(league.getName());
manager.flush();
manager.getTransaction().commit();
} catch (Exception e) {
throw new DBException(e.getMessage());
} finally {
manager.close();
}
}
@Override
public void delete(long id) throws DBException {
try {
manager = factory.createEntityManager();
League league = manager.find(League.class, id);
manager.getTransaction().begin();
manager.remove(league);
manager.flush();
manager.getTransaction().commit();
} catch (Exception e) {
throw new DBException(e.getMessage());
} finally {
// manager.close();
}
}
@Override
public void add(League item) throws DBException {
try {
manager = factory.createEntityManager();
manager.getTransaction().begin();
manager.persist(item);
manager.flush();
manager.getTransaction().commit();
} catch (Exception e) {
throw new DBException(e.getMessage());
} finally {
manager.close();
}
}
@Override
public void closeConnection() throws DBException {
factory.close();
}
}
pom.xml 持久性项目:
<?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.ucll</groupId>
<artifactId>LoLWithBuddies</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
pom.xml 对于网络项目:
<?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.ucll</groupId>
<artifactId>LoLWithBuddies-web</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>LoLWithBuddies-web</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.ucll</groupId>
<artifactId>LoLWithBuddies</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>4.0.1.RELEASE</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument-tomcat</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.servlet.jsp.jstl</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>unknown-jars-temp-repo</id>
<name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
<url>file:${project.basedir}/lib</url>
</repository>
</repositories>
</project>
示例控制器,我在我的网络应用程序中从我的域项目自动装配我的服务。此服务持有 RelationalLeagueDB class:
LoLWithBuddies-web/src/main/java/com/ucll/LoLWithBuddies/controller/LeagueController.java
package com.ucll.lolwithbuddies.controller;
import com.ucll.lolwithbuddies.domain.League;
import com.ucll.lolwithbuddies.service.Service;
import com.ucll.lolwithbuddies.service.ServiceException;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
*
* @author Christophe
*/
@Controller
@RequestMapping(value="/league")
public class LeagueController {
@Autowired
private Service service;
private boolean update=false;
@RequestMapping(method=RequestMethod.GET)
public ModelAndView getLeagues() throws ServiceException {
List<League> leagues = service.getAllLeagues();
return new ModelAndView("leagues", "leagues", leagues);
}
@RequestMapping(value="/new", method=RequestMethod.GET)
public ModelAndView getNewForm() {
update=false;
League newLeague = new League();
return new ModelAndView("leagueform", "leagueForm", newLeague);
}
@RequestMapping(method=RequestMethod.POST)
public String save(@Valid @ModelAttribute("leagueForm") League leagueForm, BindingResult result) throws ServiceException {
if (result.hasErrors()) {
return "leagueform";
}
if(update) {
service.updateLeague(leagueForm);
} else {
service.addLeague(leagueForm);
}
update=false;
return "redirect:/league";
}
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public ModelAndView getEditForm(@PathVariable long id) throws ServiceException {
update=true;
return new ModelAndView("leagueform", "leagueForm", service.getLeague(id));
}
@RequestMapping(value="/{id}/delete", method=RequestMethod.GET)
public ModelAndView getDeleteConfirmation(@PathVariable long id) throws ServiceException {
return new ModelAndView("leaguedeleteconfirm", "delete", service.getLeague(id));
}
@RequestMapping(value="/delete", method=RequestMethod.POST)
public String delete(@ModelAttribute("delete") League toDelete) throws ServiceException {
service.deleteLeague(toDelete.getId());
return "redirect:/league";
}
}
服务 bean 在基于 Java 的配置中这样配置:
@Bean(name = "service", destroyMethod = "closeConnection")
public Service service() throws ServiceException {
Service bean = new LeagueService("JPA");
return bean;
}
注意我可以将参数传递给此服务(JPA 或本地)。我想将 JPA 用于我的生产代码,但为了测试我可以传递本地参数以使用内存中的 Map 存储库,它工作正常。
谁能给我指出正确的方向?
好的,我找到问题了。正如我所说,当我第一次 运行 应用程序时,应用程序可以正常运行。但是,下次当我第一次尝试重新 运行 我的应用程序时,说因为我做了一些更改并且不得不重建,GlassFish,我的应用程序部署的服务器,不会重新部署我的应用程序,并且旧版本保留在服务器上,旧上下文,因此我的实体是 'old version of themself'。在某种程度上,我的服务 destroyMethod 似乎没有被调用。
当我强制取消部署我的应用程序,重新启动服务器,然后重新运行我的应用程序时,它会工作。但是,目前我必须手动执行此操作。现在正在研究 maven-glassfish-plugin 以自动执行此操作。