Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Persisting data problem

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm new at spring I've start reading a book from Wrox - Beginning Spring Framework.
I'm orientating from this book examples on creating my first data persisting application using maven, spring, hsqldb.
For several days I'm trying to create the application but no luck I'm getting frustrate I can't sleep . I know my post is going to be big but I'm hoping someone read it and help me make it work
I've created the model - class Album in directory /MyProject/src/main/java/model


I've also created Repository class in MyProject/src/main/java/dao directory called AlbumJpaRepository

I've created a execution file with main method AlbumDao.java in MyProject/src/main/java/dao directory

I've created two configuration XML files beans.xml and persistanceContext.xml in MyProject/src/main/resources directory

and the persistanceContext.xml


I'm starting the HSQLDB server successfully from the directory where my hsqldb-1.8.0.7.jar file is with the command
java -cp hsqldb-1.8.0.7.jar org.hsqldb.Server -database.0 temp -dbname.0 pix
and also successfully compiling the MyProject without any errors with maven. But when I try to execute the main class AlbumDao with the comand
mvn exec:java -Dexec.mainClass=dao.AlbumDao
I'm getting error ending like this

I've set it the dependencies in the pom.xml file I've also added sqltool.rc file in my home directory C:\Users\account that contains the following :

and still getting the same error
Some help
I may be on a wrong track and there is no way I can make it work with this approach or with this three softwares (Maven, Spring, HSQLDB ) for example I may also need a server Tomcat
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using Spring where are you creating your ApplicationContext?

I see in your main you just create instances of your objects directly with new, which means it isn't using Spring, just plain Java and therefore not using any of your SpringConfiguration. And therefore no JPA EntityManagerFactory being created and definitely no EntityManager being injected into your Repository. Therefore your EntityManager property in your Repository is not being set and that is a NullPointerException. And also no one is creating an instance your Repository so your dao method that uses it and calls one of its method throws a NullPointerException.

Try this in your DAO


Now another thing you should do with a Spring Application is to create interfaces for your classes and have them implement that interface and code to an interface.

Mark

 
Aleksandar Mitrev
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark that was a big help for me you saved me days of headache
 
What's a year in metric? Do you know this metric stuff tiny ad?
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic