小编Tal*_*han的帖子

单元测试JPA/Hibernate:没有EntityManager的持久性提供程序

我想为我的项目启用单元测试.我创建了如下结构.

目录结构

.
??? pom.xml
??? src
?   ??? main
?   ?   ??? java
?   ?   ??? resources
?   ?       ??? META-INF
?   ?       ?   ??? beans.xml        <-- Works fine in live
?   ?       ?   ??? persistence.xml  <-- Works fine in live
?   ??? test
?       ??? java
?       ?   ??? com
?       ?   ?   ??? test
?       ?   ?       ??? model
?       ?   ?           ??? TestEntityManagerUtil.java
?       ?   ?           ??? TestHibernate.java
?       ??? resources
?           ??? META-INF
?           ? …
Run Code Online (Sandbox Code Playgroud)

hibernate java-ee-6 jpa-2.0

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

为什么我在这个java Thread程序中获得此输出?

您好我是java编程的初学者,最近我正在学习Threads,我在这个程序的输出中遇到问题.

class s1 implements Runnable 
{ 
    int x = 0, y = 0; 
    int addX() {x++; return x;} 
    int addY() {y++; return y;} 
    public void run() { 
        for(int i = 0; i < 10; i++){ 
            System.out.println(addX() + " " + addY()); 
        }
    } 
    public static void main(String args[]) 
    { 
        s1 run1 = new s1(); 
        s1 run2 = new s1(); 
        Thread t1 = new Thread(run1); 
        Thread t2 = new Thread(run2); 
        t1.start(); 
        t2.start(); 
    } 
}
Run Code Online (Sandbox Code Playgroud)

我得到这样的输出,

1 1 2 2 1 1 …

java multithreading

4
推荐指数
2
解决办法
259
查看次数

使用Spring映射嵌套的json和POJO

我正在实现一个REST API,它使用json发送和接收数据(我对这个API设计是全新的).我使用Spring框架和requestbody/responsebody进行映射.最初,我有一个像这样的pojo:

public class Action implements Serializable {

    @Id
    private String id;
    private String name;
    private String applicationId;
    private String timeStamp;
    private String username;
    private String options;
    //Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)

这个pojo的json格式是这样的:

{
 "id": "11954cd5-eec3-4f68-b0e8-a4d9b6a976a9",
 "name": "kill button",
 "applicationId": "34fa7bbf-e49f-4f2a-933a-de26b9fdb0f1",
 "timeStamp": "2014-03-05T11:51+0000",
 "username": "user1783",
 "options": "facebook app" 
}
Run Code Online (Sandbox Code Playgroud)

这就是控制器的样子:我没有得到任何json,Spring已经转换为java对象,它应该自己手动完成吗?

@RequestMapping(value = "applications/{appId}/actions", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
@ResponseBody
public Action addAction(@PathVariable String appId, @RequestBody Action action) {
    return actionService.add(appId, action);
}
Run Code Online (Sandbox Code Playgroud)

你可以在这里找到一个漂亮的json格式:https: //gist.github.com/bakharzy/8948950

我想将json中的最后一对更改为json本身,因为它在gist中以第二种json格式显示.因此用户可以发送更多信息.现在我有一个json的新格式,这是json中的一种json,我应该如何更改pojo(私有String …

java spring json pojo

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

JPA/JPQL COUNT个问题

我有以下JPQL查询 -

SELECT f.md5 
FROM File f, Collection leafCollections, Collection instCollections 
WHERE (f.status = com.foo.bar.FileStatus.Happy OR f.status = com.foo.bar.FileStatus.Sad) 
      AND f.collectionId = leafCollections.collectionId 
      AND leafCollections.instanceCollectionId = instCollections.collectionId 
GROUP BY f.md5, instCollections.collectionId 
Run Code Online (Sandbox Code Playgroud)

它基本上返回组织在层次结构(树)中的文件的md5,这样如果相同的MD5出现在层次结构的特定分支中的多个叶子中,它将仅显示一次(由于GROUP BY).

这很好用.假设我得到了100行.每行包含一个md5作为字符串.

现在我想获得返回行的COUNT.我以为我可以做到:

SELECT COUNT(f.md5) 
FROM File f, Collection leafCollections, Collection instCollections 
WHERE (f.status = com.foo.bar.FileStatus.Happy OR f.status = com.foo.bar.FileStatus.Sad) 
      AND f.collectionId = leafCollections.collectionId 
      AND leafCollections.instanceCollectionId = instCollections.collectionId 
GROUP BY f.md5, instCollections.collectionId 
Run Code Online (Sandbox Code Playgroud)

但是,这会返回100行,每行包含一个long,表示md5出现在分支中的次数.我想要的只是获得1行,其中长值为100,即原始查询返回的总行数.我觉得我错过了一些明显的东西.

建议?

count jpql

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

使用MockMVC测试Spring 4.0.3控制器(MVC)

我正在尝试为RestController创建测试用例.这是一个简单的RestContorller

@RestController
@RequestMapping("/media")
public class MediaListController {
    @RequestMapping(method = RequestMethod.GET)
    public String getAllMedia(){
        return "TEST COMPLETE";
    }
}
Run Code Online (Sandbox Code Playgroud)

我有一个spring-rest-servlet.xml文件<mvc:annotation-driven />

<?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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.mp.web.controller.common" />

</beans>
Run Code Online (Sandbox Code Playgroud)

现在,如果我在tomcat 8上部署我的WAR,它就像预期的那样运行.

URL --> http://localhost:8080/application-name/rest/media

现在我想为这个REST服务创建一个测试用例.到目前为止我做了什么

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath:spring-rest-servlet.xml")
public class MediaTest {

    MockMvc mockMvc;

    @Before
    public void init(){
        mockMvc = MockMvcBuilders.standaloneSetup(MediaListController.class).build();
    }

    @Test
    public void getAllMediaTest1() throws Exception {
        mockMvc.perform(get("/media")).andExpect(status().isOk());
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我运行该测试,我会得到以下警告.

WARN  o.s.w.s.PageNotFound - …
Run Code Online (Sandbox Code Playgroud)

spring integration-testing spring-mvc spring-test-mvc

3
推荐指数
1
解决办法
5714
查看次数

地图可以使用 java.text.Collat​​or 吗?

我使用 SQL Server 2008 R2。

我有一个从数据库中检索到的数据缓存,目前获取字符串的方法是通过map.get(). 但是,代码的某些部分直接从数据库查询而不是仅使用缓存,因为这些情况需要一些连接。现在的问题是,collation表上的 导致通过 SQL 比较(使用表整理)的文本比较和使用map.get().

我已经阅读过,java.text.Collator但需要collator.Compare使用 才能正确使用它。有没有办法强制使用固定的Collator.

顺便说一下,这种情况下的排序规则是使用日语字符,其中在 SQL Server 中进行比较时,半角和全角日语是相同的。

java sql collation sql-server-2008

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