How to fix Hibernate LazyInitializationException: failed to lazily initialize a collection of roles, could not initialize proxy - no Session
Asked Answered
C

20

209

In the custom AuthenticationProvider from my spring project, I am trying read the list of authorities of the logged user, but I am facing the following error:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.horariolivre.entity.Usuario.autorizacoes, could not initialize proxy - no Session
    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:566)
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
    at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:545)
    at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:124)
    at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:266)
    at com.horariolivre.security.CustomAuthenticationProvider.authenticate(CustomAuthenticationProvider.java:45)
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156)
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:177)
    at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:211)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

Reading other topics from here in StackOverflow, I understand this happens due the way this type of atribute is handled by the framework, but i can't figure out any solution for my case. Someone can point what i am doing wrong and what I can do to fix it?

The code of my Custom AuthenticationProvider is:

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    private UsuarioHome usuario;

    public CustomAuthenticationProvider() {
        super();
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        System.out.println("CustomAuthenticationProvider.authenticate");

        String username = authentication.getName();
        String password = authentication.getCredentials().toString();

        Usuario user = usuario.findByUsername(username);

        if (user != null) {
            if(user.getSenha().equals(password)) {
                List<AutorizacoesUsuario> list = user.getAutorizacoes();

                List <String> rolesAsList = new ArrayList<String>();
                for(AutorizacoesUsuario role : list){
                    rolesAsList.add(role.getAutorizacoes().getNome());
                }

                List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
                for (String role_name : rolesAsList) {
                    authorities.add(new SimpleGrantedAuthority(role_name));
                }

                Authentication auth = new UsernamePasswordAuthenticationToken(username, password, authorities);
                return auth;
            }
            else {
                return null;
            }
        } else {
            return null;
        }
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }

}

My Entity classes are:

UsuarioHome.java

@Entity
@Table(name = "usuario")
public class Usuario implements java.io.Serializable {

    private int id;
    private String login;
    private String senha;
    private String primeiroNome;
    private String ultimoNome;
    private List<TipoUsuario> tipoUsuarios = new ArrayList<TipoUsuario>();
    private List<AutorizacoesUsuario> autorizacoes = new ArrayList<AutorizacoesUsuario>();
    private List<DadosUsuario> dadosUsuarios = new ArrayList<DadosUsuario>();
    private ConfigHorarioLivre config;

    public Usuario() {
    }

    public Usuario(String login, String senha) {
        this.login = login;
        this.senha = senha;
    }

    public Usuario(String login, String senha, String primeiroNome, String ultimoNome, List<TipoUsuario> tipoUsuarios, List<AutorizacoesUsuario> autorizacoesUsuarios, List<DadosUsuario> dadosUsuarios, ConfigHorarioLivre config) {
        this.login = login;
        this.senha = senha;
        this.primeiroNome = primeiroNome;
        this.ultimoNome = ultimoNome;
        this.tipoUsuarios = tipoUsuarios;
        this.autorizacoes = autorizacoesUsuarios;
        this.dadosUsuarios = dadosUsuarios;
        this.config = config;
    }

    public Usuario(String login, String senha, String primeiroNome, String ultimoNome, String tipoUsuario, String[] campos) {
        this.login = login;
        this.senha = senha;
        this.primeiroNome = primeiroNome;
        this.ultimoNome = ultimoNome;
        this.tipoUsuarios.add(new TipoUsuario(this, new Tipo(tipoUsuario)));
        for(int i=0; i<campos.length; i++)
            this.dadosUsuarios.add(new DadosUsuario(this, null, campos[i]));
    }

    @Id
    @Column(name = "id", unique = true, nullable = false)
    @GeneratedValue(strategy=GenerationType.AUTO)
    public int getId() {
        return this.id;
    }

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

    @Column(name = "login", nullable = false, length = 16)
    public String getLogin() {
        return this.login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    @Column(name = "senha", nullable = false)
    public String getSenha() {
        return this.senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }

    @Column(name = "primeiro_nome", length = 32)
    public String getPrimeiroNome() {
        return this.primeiroNome;
    }

    public void setPrimeiroNome(String primeiroNome) {
        this.primeiroNome = primeiroNome;
    }

    @Column(name = "ultimo_nome", length = 32)
    public String getUltimoNome() {
        return this.ultimoNome;
    }

    public void setUltimoNome(String ultimoNome) {
        this.ultimoNome = ultimoNome;
    }

    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name = "tipo_usuario", joinColumns = { @JoinColumn(name = "fk_usuario") }, inverseJoinColumns = { @JoinColumn(name = "fk_tipo") })
    @LazyCollection(LazyCollectionOption.TRUE)
    public List<TipoUsuario> getTipoUsuarios() {
        return this.tipoUsuarios;
    }

    public void setTipoUsuarios(List<TipoUsuario> tipoUsuarios) {
        this.tipoUsuarios = tipoUsuarios;
    }

    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name = "autorizacoes_usuario", joinColumns = { @JoinColumn(name = "fk_usuario") }, inverseJoinColumns = { @JoinColumn(name = "fk_autorizacoes") })
    @LazyCollection(LazyCollectionOption.TRUE)
    public List<AutorizacoesUsuario> getAutorizacoes() {
        return this.autorizacoes;
    }

    public void setAutorizacoes(List<AutorizacoesUsuario> autorizacoes) {
        this.autorizacoes = autorizacoes;
    }

    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name = "dados_usuario", joinColumns = { @JoinColumn(name = "fk_usuario") }, inverseJoinColumns = { @JoinColumn(name = "fk_dados") })
    @LazyCollection(LazyCollectionOption.TRUE)
    public List<DadosUsuario> getDadosUsuarios() {
        return this.dadosUsuarios;
    }

    public void setDadosUsuarios(List<DadosUsuario> dadosUsuarios) {
        this.dadosUsuarios = dadosUsuarios;
    }

    @OneToOne
    @JoinColumn(name="fk_config")
    public ConfigHorarioLivre getConfig() {
        return config;
    }

    public void setConfig(ConfigHorarioLivre config) {
        this.config = config;
    }
}

AutorizacoesUsuario.java

@Entity
@Table(name = "autorizacoes_usuario", uniqueConstraints = @UniqueConstraint(columnNames = "id"))
public class AutorizacoesUsuario implements java.io.Serializable {

    private int id;
    private Usuario usuario;
    private Autorizacoes autorizacoes;

    public AutorizacoesUsuario() {
    }

    public AutorizacoesUsuario(Usuario usuario, Autorizacoes autorizacoes) {
        this.usuario = usuario;
        this.autorizacoes = autorizacoes;
    }

    @Id
    @Column(name = "id", unique = true, nullable = false)
    @GeneratedValue(strategy=GenerationType.AUTO)
    public int getId() {
        return this.id;
    }

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

    @OneToOne
    @JoinColumn(name = "fk_usuario", nullable = false, insertable = false, updatable = false)
    public Usuario getUsuario() {
        return this.usuario;
    }

    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }

    @OneToOne
    @JoinColumn(name = "fk_autorizacoes", nullable = false, insertable = false, updatable = false)
    public Autorizacoes getAutorizacoes() {
        return this.autorizacoes;
    }

    public void setAutorizacoes(Autorizacoes autorizacoes) {
        this.autorizacoes = autorizacoes;
    }

}

Autorizacoes.java

@Entity
@Table(name = "autorizacoes")
public class Autorizacoes implements java.io.Serializable {

    private int id;
    private String nome;
    private String descricao;

    public Autorizacoes() {
    }

    public Autorizacoes(String nome) {
        this.nome = nome;
    }

    public Autorizacoes(String nome, String descricao) {
        this.nome = nome;
        this.descricao = descricao;
    }

    @Id
    @Column(name = "id", unique = true, nullable = false)
    @GeneratedValue(strategy=GenerationType.AUTO)
    public int getId() {
        return this.id;
    }

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

    @Column(name = "nome", nullable = false, length = 16)
    public String getNome() {
        return this.nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    @Column(name = "descricao", length = 140)
    public String getDescricao() {
        return this.descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }
}

Full project available on github

--> https://github.com/klebermo/webapp_horario_livre

Cathodoluminescence answered 2/4, 2014 at 19:56 Comment(5)
Fetch your authorities eagerly or use a OpenSessionInViewFilter.Borzoi
it's exactly that I trying look how to do. What I have tried was this: List<Autorizacoes> authority = user.getAutorizacoes(), inside same function from allocation of UsernamePasswordAuthenticationToken, but still don't work.Cathodoluminescence
@ManyToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)Borzoi
Ok, I try that, but still don't work. My Entity class updated: github.com/klebermo/webapp_horario_livre/blob/master/src/com/…, My current AuthenticationProvider: github.com/klebermo/webapp_horario_livre/blob/master/src/com/…Cathodoluminescence
Possible duplicate of How to solve the “failed to lazily initialize a collection of role” Hibernate exceptionCamaraderie
C
242

You need to either add fetch=FetchType.EAGER inside your ManyToMany annotations to automatically pull back child entities:

@ManyToMany(fetch = FetchType.EAGER)

A better option would be to implement a Spring transactionManager by adding the following to your Spring configuration file:

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven />

You can then add an @Transactional annotation to your authenticate() method like so:

@Transactional
public Authentication authenticate(Authentication authentication)

This will then start a db transaction for the duration of the authenticate method allowing any lazy collection to be retrieved from the db as and when you try to use them.

Cuckoopint answered 2/4, 2014 at 20:26 Comment(6)
Actually, I have the transactionManager configured in my application, and I use it in my DAO classes. If I try use in the method authenticate from AuthenticationProvider like you suggest, I get an error Caused by: java.lang.IllegalArgumentException: Can not set com.horariolivre.security.CustomAuthenticationProvider field com.horariolivre.security.SecurityConfig.authenticationProvider to $Proxy36. I get the same error if I use the add fetchType=FetchType.EAGER inside my ManyToMany annotation (and I can use this in only one atribute - I have three of same kind in my Entity class Usuario).Cathodoluminescence
Well you need to walk the child entities that you want to use within a Transaction to avoid the LazyInitializationException. Since your transactional annotation is at the dao level on a generic method you probably won't want to do that there so you will need to implement a service class in front of the dao which has the @Transactional boundaries from within which you can walk the desired child entitiesCuckoopint
Protip for someone encountering this for the future; @Transaction needs to be on a public method. If it isn't, this will not work. There may or may not be any warnings.Hiett
used the fetch type and it worked perfectly, question what is the difference in using the eager fetch to the @transactional counter partQuaver
@AustineGwa The major difference is adding the eager fetchType to the join will mean the list of child entities will always be pulled back from the DB whenever the main entity is loaded so there is a potential performance hit if there are areas of functionality which only require the data from the main entity, so using transactions and lazy loading gives you more control over the amount of data being pulled back but it completely depends on your application and use cases as to which approach is right for you.Cuckoopint
@Vlad Mihalcea has point the best solution and this solution could lead to several selects in some situations, and that is an anti-patternProprietary
V
63

The best way to handle the LazyInitializationException is to use the JOIN FETCH directive for all the entities that you need to fetch along.

Anyway, DO NOT use the following Anti-Patterns as suggested by some of the answers:

Sometimes, a DTO projection is a better choice than fetching entities, and this way, you won't get any LazyInitializationException.

Vanhomrigh answered 13/9, 2016 at 8:12 Comment(6)
fetch join is equivalent to eager fetching. Which may not be always feasible nor efficient. Also the usual way of fetching object is not via jpql queries. The fact that the open session is view is an antipattern is a long shot, and honestly, I disagree. It shall be used with caution, obviously, but there are many perfectly fine use cases that benefit from it.Behavior
No, it's NOT. Open Session in View is hack and a sign that entities are fetched even for read only projections. There is no such thing as a many perfectly fine use cases that benefit from it, no matter how hard you will try to justify it. There is no excuse for fetching more data than you really need, as well as there is no excuse for leaking data fetching outside the boundaries of the transactional service layer.Vanhomrigh
HI Vlad, Can you please explain why FETCH JOIN is not equivalent to eager loading. I am going through this article : blog.arnoldgalovics.com/2017/02/27/… . And it says "A better idea is to load the relation at the time you are loading the parent – Company – entity. This can be done with a Fetch Join" . So it is an eager loading. Isn't it?Cockney
Eager leading means adding FetchType.EAGER to your associations. JOIN FETCH is for FetchType.LAZY associations that need to be fetched eagerly at query-time.Vanhomrigh
Hi Vlad, can we perform JOIN FETCH if we are using Spring JpaRepository and not writing explicit queries?Hargrave
Entity graphs are also an option, but a Custom Repository is a much better solution.Vanhomrigh
F
40

I too had this problem when I was doing unit Testing. A very Simple Solution to this problem is to use @Transactional annotation which keeps the session open till the end of the execution.

Fitts answered 21/9, 2016 at 11:53 Comment(2)
Are you using Hibernate Transational or JPA Transactional?Bucovina
I used HibernateFitts
E
31

Adding following property to your persistence.xml may solve your problem temporarily

<property name="hibernate.enable_lazy_load_no_trans" value="true" />

As @vlad-mihalcea said it's an antipattern and does not solve lazy initialization issue completely, initialize your associations before closing transaction and use DTOs instead.

Erinn answered 10/4, 2015 at 19:54 Comment(1)
This is considered an anti-pattern by a Hibernate expert stackoverflow.com/users/1025118/vlad-mihalceaAblaze
P
15

The reason is that when you use lazy load, the session is closed.

There are two solutions.

  1. Don't use lazy load.

    Set lazy=false in XML or Set @OneToMany(fetch = FetchType.EAGER) In annotation.

  2. Use lazy load.

    Set lazy=true in XML or Set @OneToMany(fetch = FetchType.LAZY) In annotation.

    and add OpenSessionInViewFilter filter in your web.xml

Detail See my post.

https://mcmap.net/q/81061/-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session-encore-un-fois

Pertinacity answered 4/12, 2014 at 4:21 Comment(2)
OpenSessionInViewFilter is also an anti-pattern. I also suggest never setting a mapping to EAGER as there will be many cases where you don't need that data in the EAGER collection and you will be pulling down way more data than those use cases need and greatly reduce your performance. Please keep all mappings LAZY and add join fetches to your Queries instead.Mallen
"and add join fetches to your Queries instead". What does that mean?Zoba
T
15

Your Custom AuthenticationProvider class should be annotated with the following:

@Transactional

This will make sure the presence of the hibernate session there as well.

Tabitha answered 28/11, 2018 at 7:10 Comment(2)
Adding @Transactional on my Service class fixed the issue. Thanks a lot.Tillage
glad it helped!Tabitha
S
9

For those who have this problem with collection of enums here is how to solve it:

@Enumerated(EnumType.STRING)
@Column(name = "OPTION")
@CollectionTable(name = "MY_ENTITY_MY_OPTION")
@ElementCollection(targetClass = MyOptionEnum.class, fetch = EAGER)
Collection<MyOptionEnum> options;
Sigma answered 11/9, 2018 at 9:23 Comment(1)
This works for me. I also tested the option of adding @Transactional and it works also. But I choose this option.Boast
C
8

A common practice is to put a @Transactional above your service class.

@Service
@Transactional
public class MyServiceImpl implements MyService{
...
}
Carruth answered 11/9, 2020 at 17:12 Comment(0)
L
6

You can use hibernate lazy initializer.

Below is the code you can refer.
Here PPIDO is the data object which I want to retrieve

Hibernate.initialize(ppiDO);
if (ppiDO instanceof HibernateProxy) {
    ppiDO = (PolicyProductInsuredDO) ((HibernateProxy) ppiDO).getHibernateLazyInitializer()
        .getImplementation();
    ppiDO.setParentGuidObj(policyDO.getBasePlan());
    saveppiDO.add(ppiDO);
    proxyFl = true;
}
Lowland answered 16/10, 2015 at 14:49 Comment(0)
T
5

There are cases where you don't need to put @Transactional annotation to your service method, like integration testing where you can just add @Transactional to your test method. You can get org.hibernate.LazyInitializationException when testing a method that just selects from database, which does not need to be transactional. For example, when you try to load an entity class which has a lazy fetch relation like below may cause this :

@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
private List<Item> items;

so you add the @Transactional annotation to only to the test method.

@Test
@Transactional
public void verifySomethingTestSomething()  {
Topeka answered 12/2, 2021 at 15:52 Comment(0)
B
4

First of all I'd like to say that all users who said about lazy and transactions were right. But in my case there was a slight difference in that I used result of @Transactional method in a test and that was outside real transaction so I got this lazy exception.

My service method:

@Transactional
User get(String uid) {};

My test code:

User user = userService.get("123");
user.getActors(); //org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role

My solution to this was wrapping that code in another transaction like this:

List<Actor> actors = new ArrayList<>();
transactionTemplate.execute((status) 
 -> actors.addAll(userService.get("123").getActors()));
Biisk answered 11/1, 2018 at 11:36 Comment(0)
U
2

I believe rather than enabling eager fetch, it make sense to re-initialise your entity where its needed to avoid LazyInitializationException exception

Hibernate.initialize(your entity);
Uriel answered 4/11, 2017 at 1:25 Comment(0)
E
1

After changing the FetchType to EAGER, I still had the same problem. Turned out that I was using a user instance from session and the object was serialized in DB (I use Spring session JDBC), so no matter if I restarted spring boot the problem persisted. I should had requested it from the repository.

Epaulet answered 24/5, 2022 at 9:39 Comment(0)
F
0

For those using JaVers, given an audited entity class, you may want to ignore the properties causing the LazyInitializationException exception (e.g. by using the @DiffIgnore annotation).

This tells the framework to ignore those properties when calculating the object differences, so it won't try to read from the DB the related objects outside the transaction scope (thus causing the exception).

Forlini answered 9/9, 2020 at 7:40 Comment(0)
S
0
@OneToMany(mappedBy = "userId", fetch = FetchType.EAGER)

I had the same error and solved the problem.

fetch = FetchType.EAGER

Many thanks for your reply above.

Sacrament answered 29/4, 2023 at 9:43 Comment(0)
U
0

I had the same problem, by adding annotation

@ToString.Exclude

with one to many mappings helped me to solve the problem.

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@ToString.Exclude
private List<Post> posts = new ArrayList<>();
Unpeopled answered 7/11, 2023 at 6:5 Comment(0)
S
0

in my case the problem was with repository method that had the following annotation on it:

@Modifying( clearAutomatically = true, flushAutomatically = true )

clearAutomatically = true means that session will be closed

LazyInitializationException is gone when either clearAutomatically parameter or annotation itself is removed

Sn answered 4/1 at 15:55 Comment(0)
F
-1

You don't have to change the fetch type.
To fix that, we can enable the property: spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
resource: https://www.baeldung.com/hibernate-lazy-loading-workaround

Forewent answered 15/8, 2023 at 9:8 Comment(2)
This is regarded as an anti-pattern and impacts performance.Grig
At the end of text guy write: "the solution works slowly". And in bold: "Overall, this is not a production-ready feature". You do not recommend that type of thing to someone just like that.Schoolbag
S
-2

Add the annotation

@JsonManagedReference

For example:

@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name = "autorizacoes_usuario", joinColumns = { @JoinColumn(name = "fk_usuario") }, inverseJoinColumns = { @JoinColumn(name = "fk_autorizacoes") })
@JsonManagedReference
public List<AutorizacoesUsuario> getAutorizacoes() {
    return this.autorizacoes;
}
Snooker answered 9/3, 2015 at 4:26 Comment(0)
O
-2

you just need to add this to your configuration.

spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

Olmsted answered 4/1 at 3:17 Comment(1)
This copies another answer without adding anything else of value. Also be aware the answer it copies got downvoted because there are serious drawbacks to doing this.Uncinariasis

© 2022 - 2024 — McMap. All rights reserved.