小编hen*_*les的帖子

未能懒惰地初始化角色集合,..无法初始化代理 - 没有会话 - JPA + SPRING

我在Spring(3.2.2)中使用JPA(使用Hibernate 4.3.3作为持久性提供程序),我的所有字段都正常加载,但是当我尝试访问我的Collection时,它会抛出错误 -

Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.br.common.catalog.entity.Category.allParentCategoryXrefs, could not initialize proxy - no Session
    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:572)
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:212)
    at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:551)
    at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:140)
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:526)
    at java.lang.String.valueOf(String.java:2827)
    at java.io.PrintStream.println(PrintStream.java:771)
    at test.Test.main(Test.java:30)
Run Code Online (Sandbox Code Playgroud)

当我调试这个时,我的实体类中定义的每个集合都出错了 - com.sun.jdi.InvocationException occurred invoking method.

我尝试使用collection.size()和Hibernate.initialize(),但没有一个工作.在网上搜索我发现延长Persitence将解决问题,即.

 @PersistenceContext(type=PersistenceContextType.EXTENDED)

        protected EntityManager em;
Run Code Online (Sandbox Code Playgroud)

这工作得很好,但通过这个我发现他们将永远保持打开,现在春天不会处理这个.有没有办法用Spring解决这个问题.任何帮助都非常感谢.

我的实体是 -

  @Entity
    @Inheritance(strategy = InheritanceType.JOINED)
    @Table(name="CATEGORY")
    public class Category implements Serializable {
       @Id
        @GeneratedValue(generator= "CategoryId")
         @Column(name = "CATEGORY_ID")
        protected Long id;

        @ManyToOne(targetEntity = Category.class)
        @JoinColumn(name = "DEFAULT_PARENT_CATEGORY_ID") …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate jpa spring-mvc

16
推荐指数
2
解决办法
6万
查看次数

在春天打开一个弹出窗口

我有控制器,它通过它导航到home.jsp家里.jsp现在有两个按钮,FindEmployeeByid FindEmployeeByName因为我点击FindEmployeeByid新的弹出窗口打开但在弹出窗口我得到这个消息The requested resource (/EmployeeWebSpring/search/Search.jsp) is not available.,因为在Search.jsp我有使用弹簧的形式标签所以它无法获取模型对象,因此plz告诉我如何执行此操作以打开一个输入字段处于活动状态的弹出窗口,以便我可以提交数据以对该数据执行某些操作

这是我的控制器

    package com.nousinfo.tutorial.controllers;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.nousinfo.tutorial.model.EmployeeForm;
import com.nousinfo.tutorial.service.impl.EmployeeServiceImpl;
import com.nousinfo.tutorial.service.model.EmployeeBO;

@Controller
@RequestMapping("/search")
public class SearchEmployeeController {

    private EmployeeServiceImpl employeeServiceImpl;

    public void setEmployeeServiceImpl(EmployeeServiceImpl employeeServiceImpl) {
        this.employeeServiceImpl = employeeServiceImpl;
    }



    @RequestMapping(value = "/searchspring", method = RequestMethod.GET)
    public String view(@Validated EmployeeForm employeeForm)
            throws Exception {
    return "home";
    }


    @RequestMapping(value = "/employeeNo", method = RequestMethod.POST)
    public ModelAndView …
Run Code Online (Sandbox Code Playgroud)

spring-mvc

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

如果我们在数据库中只有国家代码,则获取货币代码

我有问题获得国家货币,我通过这些代码在数据库中有国家代码我必须在java中获取国家代码我如何为此制作代码请建议我.我试着用这个例子,但它不起作用.

    class Utils {
    public static SortedMap<Currency, Locale> currencyLocaleMap;
    static {
        currencyLocaleMap = new TreeMap<Currency, Locale>(
                new Comparator<Currency>() {
                    public int compare(Currency c1, Currency c2) {
                        return c1.getCurrencyCode().compareTo(
                                c2.getCurrencyCode());
                    }
                });
        for (Locale locale : Locale.getAvailableLocales()) {
            try {
                Currency currency = Currency.getInstance(locale);
                currencyLocaleMap.put(currency, locale);
            } catch (Exception e) {
            }
        }
    }

    public static String getCurrencySymbol(String currencyCode) {
        Currency currency = Currency.getInstance(currencyCode);
        System.out.println(currencyCode + ":-"
                + currency.getSymbol(currencyLocaleMap.get(currency)));
        return currency.getSymbol(currencyLocaleMap.get(currency));
    }
}

  public class GetCurrency {
    public static void main(String[] …
Run Code Online (Sandbox Code Playgroud)

java android currency

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

如何使用thymleaf检查该地图是否包含特定密钥

我正在使用thymeleaffor表示层,所以从控制器我发送包含键的映射和像这样的值列表Map<Long,List<Long>>到x.html.但我如何使用百里香检查x.html中的地图中包含的密钥,请告诉我检查它的方法

我试过这种方式,但没有成功

<span th:if="${#maps.containsKey(myMap, myStringValue)}">YEAH!</span>
Run Code Online (Sandbox Code Playgroud)

thymeleaf

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

线程同步和volatile关键字

我知道有很多主题可用于堆栈溢出但是当我试图从理论上学习volatile关键字时我相信它对我很清楚但是当我尝试运行以下示例时我的理解失败或者我无法将序列可视化.

了解挥发性 - >Making variable as volatile for an instance, makes it non cacheable for threads that means whichever thread is accessing the volatile variable it has to flush the changes into the main memory immediately so that changes should be visible to other thread.

但是在这个例子中,如果你看到输出当threadA将值写为6被计为volatile时它应该被刷新到主内存中,所以当线程B看到这个更改时它应该看到更改但是线程B显示2而不是6或7

我无法想象线程,请你帮我看一下线程执行的发生方式.

public class PerfectExampleToUnderstandVolatile {
    public static void main(String[] args) {

        ThreadSample sample = new ThreadSample();
        Thread thread = new Thread(sample,"threadA");
        Thread thread1 = new Thread(sample,"threadB");
        Thread thread2 = new Thread(sample,"threadC");
        thread.start();
        thread1.start(); …
Run Code Online (Sandbox Code Playgroud)

java multithreading

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

对象类的非final方法如何对接口可见

我对interface的一个场景很困惑.Below是我没有toString()在接口中声明方法的代码.
它是对象类的方法.但是仍然toString()可以从父类引用变量执行.但是规则说在调用子类方法之前它首先在接口中查看方法,如果方法存在则调用子类方法但在这种情况下如何toString()在没有声明的 情况下执行接口
请解释我

    public interface Parent {
    void show();
}

class Base implements Parent {
    public void show() {
        System.out.println("hey it is going to be execute");
    }

    public String toString() {
        return "itspossible";
    }

    public static void main(String[] args) {
        Parent parent = new Base();
        System.out.println(parent.toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

java

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