小编axt*_*avt的帖子

通过将Spring MVC与AJAX/JSON和MappingJackson一起使用来实现LazyInitializationException

我正在使用Spring MVC,AJAX/JSON和Hibernate从MySQL数据库中获取所有人员.我写了JUnit集成测试来验证我的服务,一切都没问题.

现在我称之为:

@RequestMapping(value="/allpersons", method=RequestMethod.GET)
public @ResponseBody Set<Person> getAllPersons() {
    Set<Person> persons= new PersonServiceImpl().getAllPersons();
    return persons;
}
Run Code Online (Sandbox Code Playgroud)

我调试了它.这条线

return persons;
Run Code Online (Sandbox Code Playgroud)

一切都好.我有一个包含所有人的HashSet.调试更多步骤,我来到这一行:

this.objectMapper.writeValue(jsonGenerator, o);
Run Code Online (Sandbox Code Playgroud)

org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
Run Code Online (Sandbox Code Playgroud)

然后我没有看到源代码,但我的调试器告诉我:

StdSerializerProvider._serializeValue(JsonGenerator, Object) line 297
Run Code Online (Sandbox Code Playgroud)

在这一行之后,我得到错误:

ERROR: org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: com.mydomain.project.dom.Person.projects, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.mydomain.project.dom.Person.projects, no session or session was closed
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)
    at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
    at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186)
    at org.codehaus.jackson.map.ser.ContainerSerializers$CollectionSerializer.serializeContents(ContainerSerializers.java:339)
    at org.codehaus.jackson.map.ser.ContainerSerializers$CollectionSerializer.serializeContents(ContainerSerializers.java:314)
    at …

java hibernate spring-mvc jackson

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

评论"实践中的并发"

@ThreadSafe
public class SynchronizedInteger {
    @GuardedBy("this") private int value;
    public synchronized int get() { return value; }
    public synchronized void set(int value) { this.value = value; }
}
Run Code Online (Sandbox Code Playgroud)

这本书说:

考虑volatile变量的好方法是想象它们的行为大致类似于清单3.3中的SynchronizedInteger类,用get和set调用替换volatile变量的读写.
......
这个比喻并不准确; SynchronizedInteger的内存可见性效果实际上比volatile变量略强.见第16章.

我检查了第16章,但没有找到确切的答案 - 内存可见性保证的确切程度如何更强?

java concurrency multithreading

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

带有X.509证书的Spring Security

我正在慢慢疯狂地尝试配置Spring Security 3.0.0来保护应用程序.

我已将服务器(jetty)配置为需要客户端身份验证(使用智能卡).但是,我似乎无法正确获取applicationContext-security.xml和UserDetailsS​​ervice实现.

首先,从应用程序上下文文件:

<?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:security="http://www.springframework.org/schema/security"
   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/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">


<security:global-method-security secured-annotations="enabled" />

<security:http auto-config="true">
    <security:intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" requires-channel="https"/>
    <security:x509 subject-principal-regex="CN=(.*?)," user-service-ref="accountService" />
</security:http>

<bean id="accountService" class="com.app.service.AccountServiceImpl"/>
Run Code Online (Sandbox Code Playgroud)

UserDetailsS​​ervice如下所示:

public class AccountServiceImpl implements AccountService, UserDetailsService {

private static final Log log = LogFactory.getLog(AccountServiceImpl.class);

private AccountDao accountDao;

@Autowired
public void setAccountDao(AccountDao accountDao) {
    this.accountDao = accountDao;
}

public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException, DataAccessException {

    log.debug("called loadUserByUsername()");
    System.out.println("called loadByUsername()");

    Account result = accountDao.getByEdpi(s); …
Run Code Online (Sandbox Code Playgroud)

java spring-security x509certificate

0
推荐指数
1
解决办法
7723
查看次数

Spring自动绑定bean属性的下拉列表

有没有办法使用spring的form.select绑定另一种bean的bean属性.例:

我有一个bean需要在视图中使用名为BeanB的属性进行更新:

public class BeanA {    
  private BeanB bean;
  private int id;

  private void setId(int id){
     this.id = id;
  }

  private int getId(){
     return this.id;
  }

  public void setBean(BeanB bean){
    this.bean = bean;
  }

  public BeanB getBean(){
   return this.bean;
  }
}

public class BeanB{
    private int id;

    private void setId(int id){
       this.id = id;
    }

    private int getId(){
       return this.id;
    }
}
Run Code Online (Sandbox Code Playgroud)

对于视图,我想发送一个使用spring的formcontroller选择的BeanB列表:

public class MyController extends SimpleFormController{

protected ModelAndView handleRenderRequestInternal(RenderRequest request, RenderResponse response) throws Exception {
  BeanA bean = …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc

0
推荐指数
1
解决办法
6024
查看次数

Hibernate中的异常(不要使用cascade ="all-delete-orphan"更改对集合的引用)

我在hibernate中遇到了一个奇怪的问题.我在我的项目中使用hibernate和spring.

问题是我有一个父子关系,当我尝试更新父亲时,我得到了异常

引起:org.hibernate.HibernateException:不要使用cascade ="all-delete-orphan"更改对集合的引用

以下是映射:

家长:

    <set name="kittens" fetch="join" lazy="false"
        inverse="true" cascade="all-delete-orphan">
        <key>
            <column name="ID" precision="22" scale="0"
                not-null="true" />
        </key>
        <one-to-many
            class="kitten" />
    </set>
Run Code Online (Sandbox Code Playgroud)

孩子:

    <composite-id name="id" class="kittenId">
         <key-property name="kittenId" type="java.lang.Long">
            <column name="Kitten_ID" precision="22" scale="0" />
        </key-property>
       <key-many-to-one name="cat" class="cat">
            <column name="ID" precision="22" scale="0" />
        </key-many-to-one>                   
    </composite-id>
Run Code Online (Sandbox Code Playgroud)

我在一个论坛中找到并试图改变

public void setKittens(Set kittens) {
    this.kittens.clear(); 
    this.kittens.addAll(kittens); 
} 
Run Code Online (Sandbox Code Playgroud)

但现在我正面临着

org.hibernate.PropertyAccessException:在小猫的setter中发生异常

任何帮助将不胜感激.

spring hibernate

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