• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

This Spring transaction Manager is always not active

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
 I use spring 5 and hibernate 5, tomcat, session factory for transaction by program and/or xml. It seems no active transaction. It seems @Transactional is useless. I even print configured transaction manager inside AppConfig, still false, even though transaction Manager is not null:


Please help!
 
Saloon Keeper
Posts: 28126
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Wan!

I don't normally configure Spring Data in Java code. I just use the application.xml bean definition file for that.

I get the impression that you are trying to use Spring Data on Legacy Hibernate and I'm not sure that there is support for that. @Transactional works just fine on Spring Data JPA using Hibernate JPA as the JPA provider, though.
 
A. Wan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,
 It was working with Hibernate Session and @Transactional use xml configuration in the past. I upgraded Spring 3 to 5, struts from 1.2 to 2, also log4j to log4j2.

A. W.
 
A. Wan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,
 I tried all, it's just no transaction, I even did it inside AppConfig for Hibernate transaction manager, it seems fine and proxy, return a valid transaction manaer but its not active t all, no way to make it.

A. W.
 
Tim Holloway
Saloon Keeper
Posts: 28126
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, as I said, I think you're using the wrong Hibernate. If your Hibernate uses "Sessions" instead of an EntityManager, then I don't know that Spring Data will work that way, You should be using Hibernate JPA.

Here's how I configure my persistence, which, as I said, I do in XML and not in Java code. The JDBC connection options come from JNDI.
 
A. Wan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any way to mimic the same function as @Transactional?

I have about 100 of them if use begin/commit for each.
 
Tim Holloway
Saloon Keeper
Posts: 28126
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really think you've scrambled both types of Hibernate in a single app and hacking it more will only make a bigger mess.

You'd be better off upgrading the legacy Hibernate code (using Sessions) to Hibernate JPA (using EntityManager).
 
A. Wan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,
  If I configure it use JPA Transaction Manager, am I still able to use Hibernate Session (Open/Current) to session.getNamedQuery ?

A.
 
Tim Holloway
Saloon Keeper
Posts: 28126
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, because legacy Hibernate and Hibernate JPA are two separate products with different APIs. You cannot safely mix them.
 
A. Wan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,
 I actually don't see anything wrong in the configuration. Could get the Configuration bean or any @Bean include transaction manager. How come SpringSynchronizationManager/@Transactional never active? There should be a way to active/deactivate it on the fly rather than just @EnableTransactionManagement.

A.
 
Tim Holloway
Saloon Keeper
Posts: 28126
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I said, you're trying to configure features for one ORM system and use them on a completely different ORM system.

Legacy Hibernate and Hibernate JPA are like Java and JavaScript. Just because they look a lot alike doesn't mean you can put Java code in a JavaScript file and expect it to work no matter how well you think you've configured it.
 
A. Wan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am I able to just use openSession() from hibernate, is there a way to auto close it in a method?

A.
 
Tim Holloway
Saloon Keeper
Posts: 28126
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again. There are two types of Hibernate.

Hibernate JPA does NOT use sessions. At all. Ever, That's only for legacy Hibernate. And you really shouldn't be using legacy Hibernate if you want Spring Data.
 
A. Wan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then how do I do getNamedQueries or native queries?

Also, all entities/beans need to be registered?

Failed to initialize JPA EntityManagerFactory: No identifier specified for entity: com.core.user.bean.User

Thanks!
 
Tim Holloway
Saloon Keeper
Posts: 28126
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both named and native queries are supported by Hibernate JPA. I've used both frequently. Well, named queries frequently. I try not to use native queries unless I can't get the job done wth JPQL.

When you use Spring Data JPA, you can provide a package path that JPA uses to scan for @Entity classes and automatically registers them. For example, I typically define my JPA Entities as classes under a path such as com.mousetech.myapp.persistence.

I don't have enough context to guess at what your third question requires. Although it might happen if User wasn't a registered Entity.
 
A. Wan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I change all of the code to use Spring Data, Am I still able to use struts2 for MVC?

Thanks!
 
Tim Holloway
Saloon Keeper
Posts: 28126
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You were already using Spring Data -- or at least garbled Spring Data annotations.

Yes, you can use Struts. Or JavaServerFaces, Or Spring Web. Or pretty much any other JEE-compatible web MVC framework. I don't recommend Struts anymore, but that's just because Struts is kind of awkward compared to the more modern MVC frameworks.
 
A. Wan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to scan entities in a jar on lib?

I put all on class path, it keeps complaint about a custom type:

Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Unable to instantiate custom type:com.platform.GenericEnumUserType

I've a lot of int in database that use enumerations, it seems each needs to convert, so troublesome!

Thanks,
A.W.
 
Tim Holloway
Saloon Keeper
Posts: 28126
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is how (using Spring configuration XML) I indicate what package path I want to be scanned for Entities:


You still have legacy Hibernate polluting your app. As I said before, Hibernate JPA does not have "Session". Any reference to "Session" when using Hibernate JPA means that your application is improperly set up.
 
A. Wan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should I map/add all entities with @Identifier, @Columns since it was using hibernate mapping?

Thanks!
 
Tim Holloway
Saloon Keeper
Posts: 28126
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a typical JPA Entity class definition. I'm using a NamedQuery here.
 
A. Wan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a lot of unmapped fields, sets, data in an entity which had been in logics, will those affect the whole project (I have to use @Transient)?

Also, <generator class="identity"/> in xml should map to @Generated ?


Happy New Year!
 
Tim Holloway
Saloon Keeper
Posts: 28126
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's not that much different from the old Hibernate.

Use @Transient for fields in an Entity that don't map to database columns. Generally, don't define @Transient properties at all. Keep that data somewhere else.

The @GeneratedValue annotation allows you to mark a column as being auto-generated and to select what value generation algorithm will be used to generate those values.
 
A. Wan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim:
1. Should I change all dao and bean to use repository and JPA CRUD?

2. Also, if I use more than one @Configuration file, do I need to initialize/register all?

3. Does authentication processUrl is default, no use or same as home / in Spring Security Configuration since Spring loadUserbyname handles it?

Thanks,
A
 
Tim Holloway
Saloon Keeper
Posts: 28126
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. It isn't essential. I've got plenty of apps that pre-date that technology that still work just fine. It's convenient, though. It is somewhat "magical", however, and I am wary of magic because it can often be hard to tell what's available.

2. "@Configuration" isn't a file, it's an annotation used to assist the framework in configuring the app. You can have many files containing @Configuration. All that's required is to let Spring know where to find them so that it can limit its package scans to the appropriate places and not waste a lot of time scanning every class in the app.

Now if you meant "application configuration" such as "application.properties", you can have multiple application configuration files in both property and YAML form, The Spring documentation has a long list of where it looks for application properties and the order it reads tham in. Which is important, since Spring has rules to determine which property value to use if the same property is defined in more than one location.

I'm not sure what you are referring to, but it sounds like part of Spring Security. Which I have gotten a lot more familiar with in the last week or two.
 
A. Wan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean I can still use dao logic without repository use ORM mapping or JPA, do I map specify datasource in config or application.properties?

I am confused as  let Spring know where to find them so that it can limit its package scans to the appropriate places , like this:

@Configuration
@Import({AppConfig.class, SecurityConfig.class, PersistenceJPAConfig.class})
public class AppInitializer implements WebApplicationInitializer {
AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
       ApplicationContext ctx = new AnnotationConfigApplicationContext(AppInitializer.class);
//must call ctx.refresh() before you can call or use the above for ctx.getBean(HelloWorld.class);
logger.error("AppInitializer getBean name:" + ctx.containsBean("SecurityConfig"));
//OMG!
       root.register(SecurityConfig.class);
//root.register(AppConfig.class);
       //root.register(PersistenceJPAConfig.class);
       //root.register(AppConfig.class);
       sc.addListener(new ContextLoaderListener(root));
root.refresh(); //AbstractApplicationContext is subclass, can also call refresh()
//logger.error("In AppInitializer getJPAConfig: " + ctx.getBean(PersistenceJPAConfig.class));
logger.error("In AppInitializer getSecurityConfig: " + ctx.getBean(SecurityConfig.class));
//logger.error("In AppInitializer getSession Factory: " + ctx.getBean(SessionFactory.class));
       sc.addFilter("securityFilter", new DelegatingFilterProxy("springSecurityFilterChain")) //already added FilterChain
         .addMappingForUrlPatterns(null, false, "/*");

Also, componentScan, I've libraries in a jar file with different folders not in current configuration files
 
Tim Holloway
Saloon Keeper
Posts: 28126
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, traditional JPA logic is still OK. Just don't use JDBC with JPA, Spring or not.

I'm not sure what your code fragment is supposed to represent, but here's all I need to set up a webapp with JPA in Spring Boot:



A Spring webapp not using Spring Boot would be a little different, and Spring Security is handled in its own @Configure'd file, but it's all pretty simple for the developer, if a bit magic.

Note that there are two scans defined here in the main class. @EntityScan tells JPA what the package path is to scan for JPA Entity classes. @ServletComponentScan has no path value, so the scan is done relative to the current (main class) package.

This might be a good article to read: https://www.baeldung.com/spring-component-scanning

Finally, be careful when chasing documentation about Spring's web, security and JPA services. Some of this stuff has evolved a lot, but the Internet is a great place to find stale information. Fortunately, Spring is fairly good about backwards compatibility if you fail to use the latest and greatest config stuff.

Signs that you've got recent docs are that the examples are usually POJO-based with no "extends" or "implements" requirements for your Spring class, use of annotations, and lambda notation in place of chained method invocations.
 
A. Wan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim,
  I hate to reuse the tread, just be easier for this one:
---------------------------------
Exception

org.springframework.security.web.firewall.RequestRejectedException: The request was rejected because the URL contained a potentially malicious String "//"
org.springframework.security.web.firewall.StrictHttpFirewall.rejectedBlacklistedUrls(StrictHttpFirewall.java:374)
org.springframework.security.web.firewall.StrictHttpFirewall.getFirewalledRequest(StrictHttpFirewall.java:336)
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:194)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:354)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:267)
---------------------------------------
      I try to add cors, in vain no matter what, it does not have any constructor-ar nor property or factory:
      <beans:bean id="corsSource" class="org.springframework.web.cors.UrlBasedCorsConfigurationSource">
   <!--beans:constructor-arg name="corsConfiuration">
<beans:bean class="org.springframework.web.cors.CorsConfiguration">
<beans:property name="allowedOrigins" value="*" />
<beans:property name="allowedMethods" value="*" />
<beans:property name="allowedHeaders" value="*" />
<beans:property name="allowCredentials" value="true" />
</beans:bean>
</beans:constructor-arg-->
   </beans:bean>

Thanks ahead!
A.

 
Bartender
Posts: 2438
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this help:
https://stackoverflow.com/questions/74146917/spring-security-the-request-was-rejected-because-the-url-contained-a-potential  ?
 
A. Wan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hinai,
 I've that in java configuration files, not sure which one worked. Is there a way to add it/switching to XML configuration since Spring (not boot) does not read configuration/scan component easily.

Thanks,
A.
 
A. Wan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
 Java configuration always complaint about Exception:Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'SecurityConfig': Unsatisfied dependency expressed through field 'httpSecurity'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.config.annotation.web.builders.HttpSecurity' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Anyone around can help?
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic