0

我有这种情况:

类用户

@Entity
@Configurable(preConstruction=true)
public class User extends AbstractBussinessObject implements Serializable {
    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
    private List<Warrior> warriors;
...

类 UserDto

public class UserDto extends AbstractDto implements Serializable{
    private List<WarriorDto> warriors;
    private String name;
    private String password;

    public UserDto() {}

    public UserDto(Long id, List<WarriorDto> warriors, String name, String password) {
        this.warriors = warriors;
        ...

类战士

@Entity
public class Warrior extends AbstractBussinessObject implements Serializable{
    @JoinColumn(name = "user_id")
    @ManyToOne
    private User user;
    ...

类战士Dto

public class WarriorDto extends AbstractDto implements Serializable{
    private User user;
    ...

WarriorServiceImpl 中的方法

@Transactional(readOnly = true)
public List<WarriorDto> getAllWarriors() {
    List<Warrior> wars = null;
    List<WarriorDto> warsDto = new ArrayList<WarriorDto>();
    try {
        wars = genericDao.getAll(Warrior.class);
        if (wars != null) {
            for (Warrior war : wars) {
                warsDto.add(createWarriorDto(war));
            }   
        }
    } catch (Exception e) {}
    return warsDto;
}

DAO 中的方法

@SuppressWarnings("unchecked")
@Override
public <ENTITY> List<ENTITY> getAll(Class<ENTITY> clazz) {
    return getEntityManager().createQuery(
    "SELECT e FROM " + clazz.getSimpleName() + " e")
    .getResultList();
}

应用程序上下文.xml

<!-- Connection pool -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="initialSize" value="2" />
        <property name="minIdle" value="2" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="databasePlatform" value="${jpa.platform}" />
                <property name="generateDdl" value="true" />
                <property name="showSql" value="true" />
            </bean>
        </property>
        <property name="packagesToScan" value="cz.sutak.game" />
    </bean>

    <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <!-- Podpora deklarativni demarkace transakci - anotace @Transactional -->
    <tx:annotation-driven transaction-manager="txManager" mode="aspectj" />

    <bean id="transactionTemplate"
        class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager">
            <ref bean="txManager" />
        </property>
    </bean>

完整代码

https://github.com/sutakjakub/game

如果我想调用WarriorService.Util.getInstance().getAllWarriors( new AsyncCallback<List<WarriorDto>>()小部件,它将失败并显示以下消息:com.google.gwt.user.client.rpc.StatusCodeException: 500 The call failed on the server; 有关详细信息,请参阅服务器日志。在 Jetty 中只有这条消息:[ERROR] 500 - POST /game/warrior (127.0.0.1) 57 bytes

没有更多的错误信息。序列化(util.List)有问题吗?你有什么想法吗?非常感谢。

编辑:这是终端中的错误消息(对不起,我没看到):

错误 LazyInitializationException - 无法延迟初始化角色集合:cz.sutak.game.client.bo.User.warriors,没有会话或会话已关闭

4

2 回答 2

1

问题是在客户端 Hibernate 无法获取您标记的属性fetch = FetchType.LAZY。在通过网络传递对象图之前,您需要在服务器端急切地获取它们 og null Hibernate 代理。

有关使 Hibernate 代理无效的提示,请参阅我在另一个线程中的回答。

干杯,

于 2013-04-04T08:50:47.770 回答
0

这里正如你所说,与它无关GWT,因为它的服务器端代码。即使在客户端 GWT 支持Util.List

来到原来的问题。它与线 wars = genericDao.getAll(Warrior.class);

访问此行时没有hibernate session打开。

于 2013-04-04T09:06:03.720 回答