This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Can't @Autowire an @Entity class and an @Transaction class

 
Ranch Hand
Posts: 165
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello All,

I have many @Entity classes and many @Transaction classes in an external jar file.

From another spring project, I want to access the @Entity and @Transaction classes.

The external classes are accessed by @Autowired in a java standalone application as follows:



The Spring-Module.xml contains the following:




These maven projects build with success, but when running it, I get:

... No matching bean of type [com.MyCompany.MyDataBO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency...

Can anyone tell me where my error is?

Thank you for your time.

Ravi




 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Classes annotated with @Entity are JPA entities - they usually represent rows in a database. These are not Spring-managed beans, so you cannot inject them. You normally also don't need to inject these classes. Why are you trying to do this?

The way you work with these classes is for example through Spring Data JPA. You create repository interfaces, and then you inject these into your program. You read and write entities from and to the database through these repositories.

See, for example, this guide: Accessing Data with JPA
 
Ravi Danum
Ranch Hand
Posts: 165
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you, Jesper. I read one of the links (couldn't get the other). The link was very helpful.

What I wanted to do was put the @Entity and @Repository classes in a jar file built with maven.

I then wanted to access these from within a different project.

I may use JEE instead. I think I can access @Entity classes from a different project.

I may need the user of the @Entity classes to be multi-threaded as in a stateless session bean.

Thank you for your reply. It has helped!
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Classes with @Entity and @Repository annotations on them are just regular classes, to use them it doesn't matter if you have those classes in your project itself or in an external JAR file.

You don't have to do anything different if those classes are in an external JAR file rather than in your project itself - just make sure the JAR is in the classpath when you compile and run your own project.

Spring does not prevent you in any way from using @Entity classes that are in an external JAR file.
 
Ravi Danum
Ranch Hand
Posts: 165
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Jesper,

Could you give a code example of how I would call an @Entity from a java standalone that's in a different project?

Would I use the EntityManager? I want to use an @Repository class to persist an @Entity class.

Could you tell me the configuration files I would need to deploy in the application server for this?

Thanks.

-Ravi
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Spring guide Accessing Data with JPA already shows you exactly how you can use a Spring Data JPA @Repository to store and retrieve @Entity objects in a database.

You do not need to do anything with an EntityManager if you use this. Spring will take care of creating and calling the EntityManager for you.

Ofcourse, your Spring configuration would need to have the correct setup to create a DataSource Spring bean to connect to the database. The tutorial uses Spring Boot, which makes this very easy; by having a dependency on the H2 database, Spring Boot automatically configures everything. If you need to use different database username / password and other settings, you can define properties in application.properties to configure this (only if you are using Spring Boot!).

If you are not using Spring Boot you'll need to do some configuration; see 19.3 Controlling database connections in the Spring reference manual.
 
This will take every ounce of my mental strength! All for a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!