我的数据库中有两个表:一个用于持久化用户,另一个用于保存他们的权限。
问题是,当我尝试删除用户时,必须删除权限,这样我才能避免收到违反约束的异常。我为此使用了 JPA 级联类型.ALL 和 orphanRemoval=true :
@OneToMany(fetch = FetchType.LAZY, mappedBy = "users",cascade = CascadeType.ALL,orphanRemoval = true)
public Set<Authorities> getAuthoritieses() {
return this.authoritieses;
}
Run Code Online (Sandbox Code Playgroud)
问题是只有当将被删除的用户是在另一个会话中创建时,hibernate 才会为权限生成删除查询:当我创建用户时,添加一些权限,然后尝试删除它,引发了违反约束的异常,即在另一个会话中创建用户时则不是这种情况(然后休眠将为权限生成删除查询)。
这是引发的异常(此处的“notification_config”指的是用户权限表和“liste_envoi”):
sept. 07, 2015 7:42:14 AM com.sun.faces.lifecycle.InvokeApplicationPhase execute
Avertissement: #{listeEnvoiBean.suppListe()}: org.springframework.dao.DataIntegrityViolationException: ERREUR: UPDATE ou DELETE sur la table « liste_envoi » viole la contrainte de clé étrangère
« notifconfiglistefk » de la table « notification_config »
Détail : La clé (id)=(5) est toujours référencée à partir de la table « notification_config ».; SQL [n/a]; …Run Code Online (Sandbox Code Playgroud) 我使用 hibernate 拦截器来拦截查询并在将它们发送到 postgresql 数据库之前对其进行更改。
对查询所做的更改特定于每个连接的用户(拦截器从用户的会话中获取用户的信息)。
问题是,由于我使用 spring 和 hibernate,拦截器是一个单例并且是在 sessionFactory 级别制作的,所以它不是线程安全的。
这是spring与hibernate相关的配置:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<property name="entityInterceptor">
<ref bean="myEntityInterceptor"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="myEntityInterceptor" class="dao.generic.HibernateInterceptor"/>
Run Code Online (Sandbox Code Playgroud)
和拦截器类:
@Component
public class HibernateInterceptor extends EmptyInterceptor {
private static final long serialVersionUID = 1L;
final static Logger logger = Logger.getLogger(HibernateInterceptor.class);
@Override
public String onPrepareStatement(String sql) {
String ps = super.onPrepareStatement(sql);
if (SecurityContextHolder.getContext().getAuthentication() != null) {
UserDetails ud = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下命令获取 JSF 应用程序范围 Bean 中文件的真实路径:
FacesContext.getCurrentInstance().getExternalContext().getRealPath(file)
Run Code Online (Sandbox Code Playgroud)
问题是在应用程序启动时初始化 bean 时getCurrentInstance()抛出一个NullPointerException:
@ManagedBean(eager = true)
@ApplicationScoped
public class EnvoiPeriodiqueApp implements Serializable {
@PostConstruct
public void initBean() {
FacesContext.getCurrentInstance().getExternalContext().getRealPath("/");
}
}
Run Code Online (Sandbox Code Playgroud)
所以我试图找到另一种方法来获取文件的真实路径而不使用getCurrentInstance()JSF 。
任何帮助将不胜感激。