小编Ale*_*lex的帖子

在DELETE请求的正文中传递数据

我有两个Spring MVC控制器方法.两者都接收请求体中相同的数据(在HTLM的格式POST形式:version=3&name=product1&id=2),但一个方法处理PUT请求和另一DELETE:

@RequestMapping(value = "ajax/products/{id}", method = RequestMethod.PUT)
@ResponseBody
public MyResponse updateProduct(Product product, @PathVariable("id") int productId) {

//...
}

@RequestMapping(value = "ajax/products/{id}", method = RequestMethod.DELETE)
@ResponseBody
public MyResponse updateProduct(Product product, @PathVariable("id") int productId) {

//...
}
Run Code Online (Sandbox Code Playgroud)

在第一种方法中,product参数的所有字段都已正确初始化.在第二个中,仅id初始化字段.其他字段是null或0.(id可能是因为id路径变量而初始化).

我可以看到该HttpServletRequest对象包含请求体(version=3&name=product1&id=2)中所有字段的值.它们只是没有映射到product参数的字段.

如何使第二种方法有效?

我也尝试使用带@RequestParam注释的参数.在处理PUT请求的方法中,它可以工作.在DELETE方法中,我得到一个例外:org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'version' is not present.

我需要在 …

spring spring-mvc

24
推荐指数
1
解决办法
2万
查看次数

在Spring + Hibernate配置中获取EntityManager

我有一个Spring MVC 4.0应用程序,我正在学习JPA.我使用Hibernate作为JPA实现.

我可以按照教程中的描述配置Hibernate .它工作正常,但我必须使用Hibernate的Session对象:

@Autowired
SessionFactory sessionFactory;

...

Session session = sessionFactory.openSession();
Run Code Online (Sandbox Code Playgroud)

现在,我想改用JPA EntityManager.我在同一个网站上遵循了教程(配置非常相似).我试图以EntityManager这种方式获得一个对象:

@PersistenceContext
EntityManager entityManager;
Run Code Online (Sandbox Code Playgroud)

我收到了一条运行时消息:

java.lang.IllegalStateException: No transactional EntityManager available
Run Code Online (Sandbox Code Playgroud)

然后,我按照这个答案中的建议,并尝试使用以下代码:

@PersistenceContext
EntityManager entityManager;

...

entityManager=entityManager.getEntityManagerFactory().createEntityManager();
Run Code Online (Sandbox Code Playgroud)

它工作了几次(大约9次重复的方法调用),然后应用程序冻结.

EntityManager进入Spring + Hibernate配置的正确方法是什么?

我现在不需要任何Spring事务功能.我只是希望能够访问EntityManager并使用JPA.

Spring/Hibernate配置文件(hibernate.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-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/test_db" />
        <property name="username" value="test" />
        <property …
Run Code Online (Sandbox Code Playgroud)

spring hibernate jpa spring-mvc

17
推荐指数
1
解决办法
4万
查看次数

使用其依赖关系构建/部署EJB .jar

我是Java EE的新手。我使用Maven,Eclipse和jBoss / WildFly。

我有一个war项目。构建项目时,其所有依赖项都打包在中的war文件中WEB-INF/lib

现在,我正在尝试创建一个ejb项目(已<packaging>ejb</packaging>在中pom.xml)。我想将其部署为一个单独的项目(而不是作为的一部分war)。当我构建ejb项目时,Maven不会在中打包任何依赖项jar

如何打包/部署ejb .jar 带有其依赖项的a

更新:.ear如果可能的话,我想避免将EJB打包。(我不想再创建一个Maven项目)。

jboss ejb jar java-ee maven

5
推荐指数
1
解决办法
1091
查看次数

在WildFly中配置/注入JMS连接工厂和主题

我想在jBoss WildFly 8.2上使用JMS做一些实验。

默认的WildFly standalone-full.xml配置文件具有以下片段:

<hornetq-server>

    <connectors>
        ...
        <in-vm-connector name="in-vm" server-id="0"/>
    </connectors>

        ...

    <jms-connection-factories>
        <connection-factory name="InVmConnectionFactory">
            <connectors>
                <connector-ref connector-name="in-vm"/>
            </connectors>
            <entries>
                <entry name="java:/ConnectionFactory"/>
            </entries>
        </connection-factory>
        ...
    </jms-connection-factories>

    <jms-destinations>
         <!-- this destination I have added myself as I need a "topic", but 
                the default configuration has only two preconfigured "queues". -->
        <jms-topic name="MyTestTopic">
            <entry name="java:/jms/topic/MyTestTopic"/>
        </jms-topic>
    </jms-destinations>

</hornetq-server>
Run Code Online (Sandbox Code Playgroud)

我试图通过以下方式将此连接工厂和此主题注入到EJB中:

@Stateless
public class JmsPublisher {

    @Resource(mappedName = "java:/ConnectionFactory")
    ConnectionFactory jmsConnectionFactory;

    @Resource(mappedName = "java:/jms/topic/MyTestTopic")
    Topic topic;
Run Code Online (Sandbox Code Playgroud)

但是我在部署时收到以下错误消息:

Operation ("deploy") failed ... Services …
Run Code Online (Sandbox Code Playgroud)

jboss jndi jms java-ee wildfly

5
推荐指数
1
解决办法
9085
查看次数

为什么LocalDate,LocalTime和Stream对象使用()的工厂方法而不是构造函数?

为什么LocalDate,LocalTime,Stream,等对象使用一个工厂方法of()而不是构造的?

我找到了为什么应该使用工厂方法而不是new 这里的解释.这个答案有很多原因,但与Java Date/Time API相关的唯一原因如下:

与构造函数不同,它们不需要在每次调用时创建新对象

作为LocalDate并且LocalTime是不可变的,使用工厂并重用现有对象而不是每次都创建新对象可能是有意义的.

这就是为什么对象喜欢LocalDateLocalTime用工厂方法创建的原因(即LocalDate.of())?还有其他原因吗?

此外,Stream对象是可变的.为什么使用工厂方法(Stream.of())来创建Stream

java factory factory-method java-8

5
推荐指数
2
解决办法
408
查看次数

在JPA中按对象的ID和版本删除对象

我正在学习JPA,并且试图在Spring MVC Web应用程序中使用它。我需要实现一种删除对象/记录的方法。当前,我具有以下方法的实现:

@Transactional
public void deleteProduct(int productId, int productVersion) {

    Product product = entityManager.find(Product.class, productId);
    product.setVersion(productVersion);
    entityManager.remove(product);
}
Run Code Online (Sandbox Code Playgroud)

productVersion用于乐观锁定。它是来自Web GUI的对象/记录的旧版本。

此方法删除数据库中的记录,但是当数据库中的记录版本不匹配时,它不会引发任何异常productVersion。(我在删除对象方面只有一个问题:用来更新记录时entityManager.merge(product),出现消息异常Row was updated or deleted by another transaction)。

Hibernate生成以下SQL查询:delete from Product where id=? and version=?,即尝试检查该version字段。

我究竟做错了什么?

另外,通过ID删除对象是否正确?我担心我的方法生成了两个SQL查询:SELECTfor entityManager.find()DELETEfor entityManager.remove()。有没有删除对象/记录的最佳方法?

产品类别

@Entity
public class Product {

    @Id
    @GeneratedValue
    protected int id;

    protected String name;

    protected BigDecimal price;

    @Version
    protected …
Run Code Online (Sandbox Code Playgroud)

spring hibernate jpa spring-mvc

4
推荐指数
1
解决办法
1万
查看次数

在JBoss/WildFly中注入EntityManager

我正在学习JPA,EJB和JBoss/WildFly.

我需要在EntityManager我的应用程序中注入一个.我试图通过以下方式进行:

@Stateless
@LocalBean
public class ProductsService implements IProductsService {

    @PersistenceContext(unitName = "myUnit")
    EntityManager entityManager;

//....
}
Run Code Online (Sandbox Code Playgroud)

我的persistence.xml档案META-INF目录中有文件.war:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="myUnit">
        <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source> 
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="create" />
            <property name="hibernate.show_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)

JNDI数据源(java:jboss/datasources/ExampleDS)是干净的WildFly安装中提供的默认数据源.

当我将我的应用程序部署到WildFly时,我收到以下错误:

JBAS011440: Can't find a persistence unit named myUnit in deployment "my-web-app.war".
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

jboss hibernate jpa java-ee wildfly

4
推荐指数
1
解决办法
6561
查看次数

标签 统计

hibernate ×3

java-ee ×3

jboss ×3

jpa ×3

spring ×3

spring-mvc ×3

wildfly ×2

ejb ×1

factory ×1

factory-method ×1

jar ×1

java ×1

java-8 ×1

jms ×1

jndi ×1

maven ×1