BeanValidation to check if delete is possible - spring

How can I use BeanValidation to validate record before delete? For example I can only delete record when executeDate field is in the future.
I'm using with success beanvalidation in my project. It's annotation based configured. I use it by #Valid annotation on Controller method. It's Spring based MVC application.
my environment:
<hibernate4.core.version>4.2.1.Final</hibernate4.core.version>
<spring.version>3.2.2.RELEASE</spring.version>
<hibernate.search.version>4.2.0.Final</hibernate.search.version>
Edit:
I have seen this: hibernate validator - different groups on create, update, delete, and Hibernate Documentation also, but it is steel not clear for me.
If I have validation working so far do I need only add special case for delete? I'm confused what steps I need to do for get only this one feature.

add to your persistence configuration
<property name="javax.persistence.validation.group.pre-remov">MyDeleteGroup</property>
Add this annotation to your date field
#Past(groups=MyDeleteGroup.class)
Delete Group MyDeleteGroup is just a marker interface like javax.validation.groups.Default

For Spring Boot users:
application.yml
spring:
jpa:
properties:
javax:
persistence:
validation:
group:
pre-remove: com.your_package.MyDeleteGroup
or
application.proeprties
spring.jpa.properties.javax.persistence.validation.group.pre-remove=com.your_package.MyDeleteGroup

see this link for BeanValidation documentation
you can use #Past annotation for your case
see section 6.1
<event type="pre-delete">
<listener class="org.hibernate.cfg.beanvalidation.BeanValidationEventListener"/>
</event>

Related

Hibernate configuration property not supported by Quarkus application.properties

I am using Quarkus 2.9.2.Final and would like to set a Hibernate config parameter, which is not one of the "chosen ones" which can go into Quarkus' application.properties as per the documentation.
Specifically I would like to set this Hibernate configuration property:
<property name="hibernate.hql.bulk_id_strategy"
value="org.hibernate.hql.spi.id.inline.InlineIdsInClauseBulkIdStrategy"
/>
to prevent Hibernate from generating temporary tables (as described here).
How can I achieve this?
Have you tried the quarkus.hibernate-orm.unsupported-properties (ref) config?

Disable Envers in properties file

I have already referred to this question: Spring Boot 2 - disable Envers, but the answer did not work for me.
I am using Spring Boot and would like to disable Envers in the properties file without having to go into my code and remove any #Audited annotations or such.
I have tried the following to no avail:
hibernate.integration.envers.enable=false
spring.jpa.properties.hibernate.integration.envers.enable=false
spring.jpa.properties.org.hibernate.integration.envers.enable=false
hibernate.integration.envers.enabled=false
spring.jpa.properties.hibernate.integration.envers.enabled=false
spring.jpa.properties.org.hibernate.integration.envers.enabled=false
hibernate.envers.autoRegisterListeners=false
spring.jpa.properties.org.hibernate.envers.autoRegisterListeners=false
I cannot speak specifically to the integration with Spring Boot, but you should be able to force it to be disabled by supplying a custom hibernate.properties file shown below:
# this disables hibernate envers, even if its on the classpath
hibernate.integration.envers.enabled=false

Spring Boot 2 - disable Envers

How can I disable Hibernate Envers in Spring Boot 2? I don't want to remove the dependency, because the code relies on it depending on the environment.
I tried the following properties in my application.properties but none worked. On startup it always prints envers enabled = true
spring.hibernate.integration.envers.enabled=false
hibernate.integration.envers.enabled=false
spring.jpa.hibernate.integration.envers.enabled=false
Can you try;
spring.jpa.properties.hibernate.integration.envers.enabled=false
"Non-Spring Data JPA" Hibernate properties are configured through spring.jpa.properties.hibernate.* afaik. That might just do it!
Actually there are two ways to disable hibernate envers in spring boot applications:
Wih VM Arguments:
-Dhibernate.integration.envers.enabled= false
With hibernate.properties (not application.properties) file, (if not exist, create) add the line below.
hibernate.integration.envers.enabled=false

How to deal with defaultRolePrefix="ROLE_" in Spring Security update from 3.2.7 to 4.0.2.RELEASE

My Spring Boot application works on Spring Security 3.2.7.RELEASE.
Now, I'd like to update it to 4.0.2.RELEASE.
After hours of debug I have found that Spring Security 4.0.2.RELEASE uses defaultRolePrefix="ROLE_"
in
org.springframework.security.access.expression.SecurityExpressionRoot.hasAnyAuthorityName(String prefix, String... roles) method
In my application I use roles without this prefix and accordingly I get AccessDeniedException.
How to configure Spring Boot in order to use SecurityExpressionRoot.defaultRolePrefix="" ?
I found the solution how to fix it. I need to change hasRole to hasAuthority, for example:
#PreAuthorize("hasAuthority('PERMISSION_CREATE_NODE')")
In the other hand you can remove role prefix ass described here. In this cas you are free to use other annotations.

Spring Boot, Hibernate Search properties

How to provide Hibernate Search parameters when using Spring Boot?
...
spring.datasource.driverClassName=org.postgresql.Driver
hibernate.search.jmx_enabled=true
hibernate.search.default.directory_provider=filesystem
hibernate.search.generate_statistics=true
hibernate.search.lucene_version=LUCENE_CURRENT
hibernate.search.default.indexBase=/mypath-to-index
It does not care what I provide. Default settings always get applied.
I think below code does not have anything to process properties related to Hibernate Search. Can that be the issue?
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaProperties.java
You can put them in the application.properties file if you put "spring.jpa.properties." in front of the property names.
Example:
spring.jpa.properties.hibernate.search.jmx_enabled=true
spring.jpa.properties.hibernate.search.default.directory_provider=filesystem
spring.jpa.properties.hibernate.search.generate_statistics=true
spring.jpa.properties.hibernate.search.lucene_version=LUCENE_CURRENT
spring.jpa.properties.hibernate.search.default.indexBase=/mypath-to-index
Spring will take any properties under spring.jpa.properties.* and pass them along (with the prefix stripped) once the EntityManagerFactory is created.
Got it working.
Put another property file named "hibernate.properties" inside src/main/resources with below content.
hibernate.search.jmx_enabled=true
hibernate.search.default.directory_provider=filesystem
hibernate.search.generate_statistics=true
hibernate.search.lucene_version=LUCENE_CURRENT
hibernate.search.default.indexBase=/mypath-to-index

Resources