Tag: spring

@OneToMany删除子项

我有两个这样的实体: public class Client { … @OneToMany(mappedBy = “client”, cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.REMOVE },orphanRemoval = true, fetch=FetchType.EAGER) @Fetch(value = FetchMode.SUBSELECT) @Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN }) private List companyContracts; … } 公司合同: public class CompanyContract { … @ManyToOne @JoinColumn(name = “IDCLIENT”) private Client client; … } 我想删除像这样的companyContract: client.getCompanyContracts().remove(companyContract) client = clientService.updateClient(client); 但删除过程没有发生 我究竟做错了什么 ? […]

创建Json字符串

我使用Jackson库生成json字符串,如下所示: ObjectMapper objectMapper = new ObjectMapper(); String str = objectMapper.writeValueAsString(model); 而这个剪切代码例如生成这样的somtething: {“x” : “This is x”, “y” : “This is y”} 但我想生成这样的东西: {‘x’ : ‘This is x’, ‘y’ : ‘This is y’} 我的意思是如何用单引号字符串更改双引号字符串。我尝试更改这样的代码: ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); String str = objectMapper.writeValueAsString(model); 但是这个剪切代码生成了第一个。 当然我可以用替换方法处理这个问题,但我希望jackson图书馆为我做这个。 我该如何处理这个问题?

使用Spring Boot 1.4从Spring Boot Test中排除Spring集成

我最近开始使用Spring Boot 1.4和新的Spring Boot Testfunction。 我正在使用spring-boot-starter-integration与Spring Boot 1.4.2.RELEASE并尝试使用新的@DataJpaTest测试我的存储库 当我运行我的测试时,我得到一个关于没有限定bean的exception。 有问题的bean是我的集成bean的handler 。 如何在JPA测试期间排除集成运行? 申请类别: import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @ImportResource(“integration.xml”) public class AutomateResultConsumerApplication { public static void main(String[] args) { SpringApplication.run(AutomateResultConsumerApplication.class, args); } } 测试类: @RunWith(SpringRunner.class) @DataJpaTest public class SampleHistoryRepositoryTest { @Autowired private TestEntityManager entityManager; @Autowired private SampleHistoryRepository sampleHistoryRepository; @Test public void findAllByObjectIdAndField() throws […]

LazyInitializationException,同时使用TestNG对Spring中使用的Hibernate实体类进行unit testing

在我的Spring配置中,我已经要求会话在我的视图中保持打开状态: 但是,这个bean似乎并不认为我的TestNGunit testing是一种观点。 ;-)那没关系,但是unit testing是否有类似的bean,以便在unit testing时避免可怕的LazyInitializationException? 到目前为止,我的一半unit testing因此而死亡。 我的unit testing通常如下所示: @ContextConfiguration({“/applicationContext.xml”, “/applicationContext-test.xml”}) public class EntityUnitTest extends AbstractTransactionalTestNGSpringContextTests { @BeforeClass protected void setUp() throws Exception { mockEntity = myEntityService.read(1); } /* tests */ @Test public void LazyOneToManySet() { Set entities = mockEntity.getSomeEntitySet(); Assert.assertTrue(entities.size() > 0); // This generates a LazyInitializationException } } 我已经尝试将setUp()更改为: private SessionFactory sessionFactory […]

在Spring Boot服务上运行有限数量的线程

我目前正在使用spring boot开发一个Web应用程序,我在服务层中遇到了问题。 我的服务层上有一个沉重的方法。 如果多个用户调用相同的服务,则应用程序会因内存不足而停止。 所以我想限制该方法的并行运行线程的数量。 到目前为止,我已经出现了在该方法上使用synchronized 。 但它会将它限制为单线程方法。 @Service public class DocumentService{ private synchronized void doReplacement(){ //should have limited no of multi threads (eg. 3) } private void normalMethod(){ //no restrictions } } 我该怎么做才能完成这项任务。 任何帮助,将不胜感激。

Spring MVC配置引发奇怪的exception

我正在尝试部署一个新的Spring MVC应用程序,我已经完成了十几次,但现在我运行了一个非常奇怪的错误,甚至无法弄清楚发生了什么: 我的javaee-api与servlet-api冲突。 在控制台中它写道: INFO: validateJarFile(E:\development\workspace\conference\src\main\webapp\WEB- INF\lib\javaee-api-6.0.jar) – jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class 嗯,是的,这是一个警告,但这个jar子没有加载,我需要它。 另外,我的应用程序库中没有servlet-api jar等。 此外,控制台抛出这样的exception: SEVERE: Error configuring application listener of class com.sun.faces.config.ConfigureListener java.lang.ClassNotFoundException: com.sun.faces.config.ConfigureListener 我的意思是,这个jar是指JSF和所有这些东西。 我根本不使用它,它应该在哪里尝试这样的课程? O_O 我在tomcat 7上运行应用程序 有什么想法发生了什么?

如何在spring mvc拦截器中validation会话

我是spring framework的新手。在我的代码中,我使用拦截器检查会话是否存在。如果会话存在,我允许调用控制器,否则我重定向登录页面。 以下是我的代码。 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Users user=(Users) session.getAttribute(“user”); if(user == null) { System.err.println(“Request Path : “); response.sendRedirect(“index”); return false; } else { return true; } } 但是这段代码没有成功重定向。我收到以下错误, In Mozilla i get below error The page is not redirecting properly In chorme i get below error? This web page has redirect […]

使用外键字段持久化实体时出错

我试着坚持数据库这个实体: @Entity @Table(name=”pessoa_juridica”) public class PessoaJuridica { @Id @Column(name = “id”) @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; @Column(name=”cnpj”) @Order(value=1) private String cnpj; @Column(name=”razao_social”) @Order(value=2) private String razaoSocial; @OneToOne( fetch = FetchType.EAGER, cascade = {CascadeType.ALL} ) @Order(value=3) @JoinColumn(name=”contato”) private Contato contato; } 但是当我用表单打开视图时,我收到此错误: org.springframework.beans.NullValueInNestedPathException: Invalid property ‘pessoaJuridica.contato’ of bean class [com.spring.loja.model.cliente.persistence.model.Cliente]: Could not instantiate property type [java.lang.Integer] to […]

Spring mvc @DateTimeFormat无法按预期工作

我刚刚在jsp中制作的简单页面中添加了一个jQuery日期选择器。 使用Spring mvc 4.0.0。 我想在我的模型类中有java.util.Date字段,让spring将日期String从前端转换为日期。 我的问题是,如果我有 @DateTimeFormat(pattern = “dd/MM/yy”) private Date startDate; 在我的模型类中,我在spring控制器中收到null值。 (startDate setter既不被调用)如果我将startDate更改为一个简单的String,那么正确填充该值并且在控制器中我能够检索startDate字符串。 你能告诉我我错过了什么吗? 试图按照http://gerrydevstory.com/2013/05/21/binding-date-form-input-on-spring-mvc/似乎没有使用其他技巧将String转换为Date。

传递Spring数据中的参数

我想知道如何将参数传递给bean.xml。 如果我在bean.xml中这样写,它按预期工作 但如果我这样写,我会收到错误 我的bean.xml AgingScheduler JobParametersBuilder builder = new JobParametersBuilder(); builder.addDate(“date”, new Date()); builder.addString(“fileName”, “AgingReporting_” + PropertiseUtil.settlementDateyyyyMMdd()); builder.addString(“edcbatchStatus”,”A”).toJobParameters(); 错误 Job failed with following exceptions exception :Failed to initialize the reader