相关疑难解决方法(0)

如何解决"未能懒散地初始化角色集合"的Hibernate异常

我有这个问题:

org.hibernate.LazyInitializationException:懒得初始化角色集合:mvc3.model.Topic.comments,没有会话或会话被关闭

这是模型:

@Entity
@Table(name = "T_TOPIC")
public class Topic {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @ManyToOne
    @JoinColumn(name="USER_ID")
    private User author;

    @Enumerated(EnumType.STRING)    
    private Tag topicTag;

    private String name;
    private String text;

    @OneToMany(mappedBy = "topic", cascade = CascadeType.ALL)
    private Collection<Comment> comments = new LinkedHashSet<Comment>();

    ...

    public Collection<Comment> getComments() {
           return comments;
    }

}
Run Code Online (Sandbox Code Playgroud)

调用模型的控制器如下所示:

@Controller
@RequestMapping(value = "/topic")
public class TopicController {

    @Autowired
    private TopicService service;

    private static final Logger logger = LoggerFactory.getLogger(TopicController.class);


    @RequestMapping(value = "/details/{topicId}", method = RequestMethod.GET)
    public ModelAndView …
Run Code Online (Sandbox Code Playgroud)

java spring jsp hibernate spring-mvc

340
推荐指数
21
解决办法
46万
查看次数

为什么Hibernate Open Session in View被认为是一种不好的做法?

您使用什么样的替代策略来避免LazyLoadExceptions?

我确实理解在视图中打开会话有以下问题:

  • 分层应用程序在不同的jvm中运行
  • 事务只在最后提交,很可能你以前想要结果.

但是,如果您知道您的应用程序在单个虚拟机上运行,​​为什么不通过在视图策略中使用开放会话来减轻您的痛苦?

java hibernate jpa lazy-loading open-session-in-view

104
推荐指数
5
解决办法
6万
查看次数

Hibernate:拉动所有懒惰集合的最佳实践

是)我有的:

@Entity
public class MyEntity {
  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
  @JoinColumn(name = "myentiy_id")
  private List<Address> addreses;

  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
  @JoinColumn(name = "myentiy_id")
  private List<Person> persons;

  //....
}

public void handle() {

   Session session = createNewSession();
   MyEntity entity = (MyEntity) session.get(MyEntity.class, entityId);
   proceed(session); // FLUSH, COMMIT, CLOSE session!

   Utils.objectToJson(entity); //TROUBLES, because it can't convert to json lazy collections
}
Run Code Online (Sandbox Code Playgroud)

真是个问题:

问题是我不能在会话关闭后拉懒惰集合.但我也不能在proceed方法中关闭会话.

什么解决方案(粗解决方案):

a)在会话关闭之前,强制hibernate拉取惰性集合

entity.getAddresses().size();
entity.getPersons().size();
Run Code Online (Sandbox Code Playgroud)

....

b)使用@Fetch(FetchMode.SUBSELECT) …

java hibernate lazy-loading

77
推荐指数
5
解决办法
11万
查看次数

为什么我们不应该制作一个Spring MVC控制器@Transactional?

关于这个主题已经有一些问题,但是没有任何响应可以提供参数来解释为什么我们不应该创建一个Spring MVC控制器Transactional.看到:

所以为什么?

  • 不可逾越的技术问题吗?
  • 有架构问题吗?
  • 是否存在性能/死锁/并发问题?
  • 有时需要多个单独的交易吗?如果是,有什么用例?(我喜欢简化设计,调用服务器要么完全成功要么完全失败.这听起来是一个非常稳定的行为)

背景:几年前,我在团队中使用C#/ NHibernate/Spring.Net实现了一个相当大的ERP软件.对服务器的往返实际上是这样实现的:事务在进入任何控制器逻辑之前打开,并在退出控制器后被提交或回滚.该交易在框架中进行管理,因此没有人必须关心它.这是一个出色的解决方案:稳定,简单,只有少数架构师不得不关心交易问题,团队的其他成员只是实现了功能.

从我的角度来看,这是我见过的最好的设计.当我尝试使用Spring MVC重现相同的设计时,我进入了一个带有延迟加载和事务问题的噩梦,每次都有相同的答案:不要让控制器进行事务处理,但为什么呢?

提前感谢您的答案!

java controller spring-mvc transactional

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

JPA 延迟加载在 Spring Boot 中不起作用

我在谷歌上搜索了很多,Spring Boot(最新版本)可能没有延迟加载不起作用,这真的很奇怪。以下是我的代码片段:

我的资源:

 public ResponseEntity<Page<AirWaybill>> searchAirWaybill(CriteraDto criteriaDto, @PageableDefault(size = 10) Pageable pageable{
airWaybillService.searchAirWaybill(criteriaDto, pageable);
        return ResponseEntity.ok().body(result);
}
Run Code Online (Sandbox Code Playgroud)

我的服务:

@Service
@Transactional
public class AirWaybillService {

//Methods

 public Page<AirWaybill> searchAirWaybill(AirWaybillCriteriaDto searchCriteria, Pageable pageable){
    //Construct the specification
            return airWaybillRepository.findAll(spec, pageable);
   }
}
Run Code Online (Sandbox Code Playgroud)

我的实体:

@Entity
@Table(name = "TRACKING_AIR_WAYBILL")
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@airWaybillId") //to fix Infinite recursion with LoadedAirWaybill class
public class AirWaybill{
//Some attributes
    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "FK_TRACKING_CORPORATE_BRANCH_ID")
    private CorporateBranch corporateBranch;
}
Run Code Online (Sandbox Code Playgroud)

并且在调试时,我仍然会加载所有延迟加载的属性。见下图。

在此处输入图片说明

我的问题之一是杰克逊会参与这种行为吗?有什么方法我可能错过了激活延迟加载?

编辑

另一个问题,调试器是否会参与破坏延迟加载?

编辑2:

对于规范构建,我有:

public static Specification<AirWaybill> …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate spring-data-jpa spring-boot

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

如何判断我是否在Spring中处理事务?

我在Spring服务中存在持久性问题.休息控制器R接收请求,并通过一些代码层以复杂的方式调用服务S. S中的服务方法声明一个事务; 这应该是第一次启动交易.但是服务的行为就像之前已经启动的事务一样,并且我的一些数据对象将成为会话的一部分(他们不能这样做).如何确定交易是否已处于活动状态?我试图注入EntityManager和/或JpaTransactionManager; 但两者似乎没有任何帮助.

如何检查我是否在交易中?

在我寻找可能的嫌疑人的所有这些层次之前,我想确定这一点.

java spring transactions spring-data-jpa

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

手动更改多租户会话

我需要创建一个多租户应用程序,能够在我的 java 代码中的模式之间切换(不基于用户请求)。

我读过文章: https ://fizzylogic.nl/2016/01/24/make-your-spring-boot-application-multi-tenant-aware-in-2-steps/ http://www.greggbolinger。 当架构在 Rest-request 中传递时,com/tenant-per-schema-with-spring-boot/解决方案工作正常。

但是我需要实现以下逻辑:

public void compare(String originalSchema, String secondSchema){
    TenantContext.setCurrentTenant(originalSchema);
    List<MyObject> originalData = myRepository.findData();

    TenantContext.setCurrentTenant(secondSchema);
    List<MyObject> migratedData = myRepository.findData();
}
Run Code Online (Sandbox Code Playgroud)

关键是,当我手动设置 TenenantContext 时,该连接不会切换。MultiTenantConnectionProviderImpl.getConnection 仅在第一次调用我的存储库时调用。

 @Component
 public class MultiTenantConnectionProviderImpl implements  MultiTenantConnectionProvider {

     @Override
     public Connection getConnection(String tenantIdentifier) throws SQLException {
          final Connection connection = getAnyConnection();
          try {
               connection.createStatement().execute( "ALTER SESSION SET CURRENT_SCHEMA = " + tenantIdentifier );
          }
          catch ( SQLException e ) {
              throw new HibernateException(
      "Could not alter JDBC …
Run Code Online (Sandbox Code Playgroud)

spring hibernate spring-data-jpa spring-boot

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

Spring Boot + JPA:@Transactional 注释:回滚不起作用

我正在将 @Transactional 与 spring boot 和 jpa 一起使用。但它不起作用。有人可以帮忙吗。

我的插入在 myDAO 中,它在服务类中自动装配。下面的代码是实现服务接口的服务类的方法

class MyService implements Service {

@Transactional(rollbackFor = RuntimeException.class)
    public ResponseVO createOrder(params) {
    myDAO.insertInTable1(param);
    myDAO.insertInTable2(param);//I kept wrong table name in this query such that it throws exception
         }

 }
Run Code Online (Sandbox Code Playgroud)

java spring jpa spring-boot

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