我是Java Persistence API和Hibernate的新手.
Java Persistence API FetchType.LAZY和之间的区别是什么FetchType.EAGER?
请帮助我了解在哪里使用常规JOIN以及JOIN FETCH的位置.
例如,如果我们有这两个查询
FROM Employee emp
JOIN emp.department dep
Run Code Online (Sandbox Code Playgroud)
和
FROM Employee emp
JOIN FETCH emp.department dep
Run Code Online (Sandbox Code Playgroud)
它们之间有什么区别吗?如果是的话,哪一个使用的时候?
您使用什么样的替代策略来避免LazyLoadExceptions?
我确实理解在视图中打开会话有以下问题:
但是,如果您知道您的应用程序在单个虚拟机上运行,为什么不通过在视图策略中使用开放会话来减轻您的痛苦?
如果有人可以解释这个注释的作用以及我们何时使用它:
@Transactional(propagation=Propagation.REQUIRED)
Run Code Online (Sandbox Code Playgroud)
谢谢
我在Spring Boot文档中看到了JPA配置的这个属性spring.jpa.open-in-view=true.那么它被设置为true,它是默认值吗?
它到底意味着什么?我没有找到任何解释这种行为.
它应该是使用Hibernate SessionFactory来代替EntityManagerFactory?如果是,我该如何设置EntityManagerFactory?
感谢任何帮助.
谢谢!
在JPA中有一种方法可以在Entity类中映射枚举的集合吗?或者唯一的解决方案是使用另一个域类包装Enum并使用它来映射集合?
@Entity
public class Person {
public enum InterestsEnum {Books, Sport, etc... }
//@???
Collection<InterestsEnum> interests;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Hibernate JPA实现,但当然更喜欢实现不可知的解决方案.
在我的spring项目的自定义AuthenticationProvider中,我正在尝试读取已登录用户的权限列表,但我遇到以下错误:
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) …Run Code Online (Sandbox Code Playgroud) Transaction-scoped Persistence上下文和Extended Persistence上下文之间有什么区别?
我在堆栈溢出文章中看到很多评论我发现@Transactional与@Service或@Controller一起使用的某些事情
"通常,应该在服务层进行交易."
"正常情况是在服务层级注释"
"认为事务属于服务层.它是了解工作单元和用例的人.如果你有几个DAO注入到需要在单个事务中协同工作的服务,那么这是正确的答案." [资源]
使用带有@service层的@transactional的缺点
如果我有2个方法,例如saveUser()和saveEmail()(因为我将电子邮件存储在数据库中以便稍后发送 - 就像一个队列)我会在我的服务中创建一个方法saveUserAndSendEmail(用户用户),它将是事务性的.[资源]
这意味着我在服务层创建了许多方法,而不是一个Save Generic Method,如下所示
public <T> long save(T entity) throws DataAccessException {
Session session = sessionFactory.getCurrentSession();
long getGenVal=(Long) session.save(entity);
return getGenVal;
}
Run Code Online (Sandbox Code Playgroud)
根据上面的解决方案,这意味着我们有很多方法,如以下LOL ..
public <T> long saveAccount(T entity)....
public <T> long saveWithAuditLog(T entity, K entity1)....
public <T> long saveWithAuditLogAndEntries(T entity, K entity, M entity)....
克服这种情况
我在@Controller中使用@Transactional,只需制作一个通用保存方法,并使用这个简单的保存方法保存所有实体/模型.如果任何方法无法保存,则控制器中的所有事务都会成功回滚.
确保@Transactional应与@Controller一起使用的其他情况
在@Controller中:
pt.save(entity1);
pt.save(entity2);
int a = 2/0;
pt.save(entity3);
Run Code Online (Sandbox Code Playgroud)
如果@Transactional on Service,前2个实体成功保存,但第三个不回滚所有事务
如果@Tratroller上的@Transactional,所有事务回滚都会发生异常
为什么堆栈溢出问道,"不要在控制器中进行事务处理.将它们放在服务层类中."? [资源]
Foo看起来有这个:
@ManyToMany
private Set<User> favouritedBy;
Run Code Online (Sandbox Code Playgroud)
用户有这个:
@ManyToMany(mappedBy = "favouritedBy")
private Set<Foo> favourites = new HashSet<Foo>();
public Set<Foo> getFavourites() {
return favourite;
}
Run Code Online (Sandbox Code Playgroud)
并且fooService具有此功能,通过tranactional方法在会话打开时访问lazyloaded集合:
@Transactional(readOnly = true)
public Set<Foo> getFavourites(User user) {
user = dao.get(User.class, user.getId()); //the dao gets a session
Set<Foo> favourites = user.getFavourites();//but the session is not here and the exception is thrown?
return favourties;
}
Run Code Online (Sandbox Code Playgroud)
编辑 修复它,不使用条件:
Set<Foo> favourites = new HashSet<Foo>(user.getFavourites());
Run Code Online (Sandbox Code Playgroud)
这用标准修复了它
Session session = sessionFactory.getCurrentSession();
final Criteria crit = session.createCriteria(Foo.class);
crit.setFetchMode("favourites", FetchMode.JOIN);
crit.add(Property.forName("id").eq(id));
return (Foo) crit.uniqueResult();
Run Code Online (Sandbox Code Playgroud) java ×9
hibernate ×6
jpa ×5
spring ×4
spring-mvc ×2
annotations ×1
controller ×1
fetch ×1
hql ×1
java-ee ×1
join ×1
lazy-loading ×1
orm ×1
service ×1
spring-boot ×1
spring-data ×1