• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

This Spring transaction Manager is always not active

 
Greenhorn
Posts: 14
  • Number of slices to send:
    Optional 'thank-you' note:
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: 27851
196
  • Number of slices to send:
    Optional 'thank-you' note:
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: 14
  • Number of slices to send:
    Optional 'thank-you' note:
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: 14
  • Number of slices to send:
    Optional 'thank-you' note:
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: 27851
196
  • Number of slices to send:
    Optional 'thank-you' note:
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: 14
  • Number of slices to send:
    Optional 'thank-you' note:
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: 27851
196
  • Number of slices to send:
    Optional 'thank-you' note:
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: 14
  • Number of slices to send:
    Optional 'thank-you' note:
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: 27851
196
  • Number of slices to send:
    Optional 'thank-you' note:
No, because legacy Hibernate and Hibernate JPA are two separate products with different APIs. You cannot safely mix them.
 
A. Wan
Greenhorn
Posts: 14
  • Number of slices to send:
    Optional 'thank-you' note:
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: 27851
196
  • Number of slices to send:
    Optional 'thank-you' note:
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: 14
  • Number of slices to send:
    Optional 'thank-you' note:
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: 27851
196
  • Number of slices to send:
    Optional 'thank-you' note:
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: 14
  • Number of slices to send:
    Optional 'thank-you' note:
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: 27851
196
  • Number of slices to send:
    Optional 'thank-you' note:
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: 14
  • Number of slices to send:
    Optional 'thank-you' note:
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: 27851
196
  • Number of slices to send:
    Optional 'thank-you' note:
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: 14
  • Number of slices to send:
    Optional 'thank-you' note:
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: 27851
196
  • Number of slices to send:
    Optional 'thank-you' note:
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: 14
  • Number of slices to send:
    Optional 'thank-you' note:
Should I map/add all entities with @Identifier, @Columns since it was using hibernate mapping?

Thanks!
 
Tim Holloway
Saloon Keeper
Posts: 27851
196
  • Number of slices to send:
    Optional 'thank-you' note:
Here's a typical JPA Entity class definition. I'm using a NamedQuery here.
 
A. Wan
Greenhorn
Posts: 14
  • Number of slices to send:
    Optional 'thank-you' note:
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: 27851
196
  • Number of slices to send:
    Optional 'thank-you' note:
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: 14
  • Number of slices to send:
    Optional 'thank-you' note:
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: 27851
196
  • Number of slices to send:
    Optional 'thank-you' note:
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: 14
  • Number of slices to send:
    Optional 'thank-you' note:
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: 27851
196
  • Number of slices to send:
    Optional 'thank-you' note:
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |