5

In my java project, I have 2 entities with same name but different package, also I have corresponding dao for these entities.

Now because of 2 entities with same name, it was giving duplicate scan error, and so I added name attribute to these entities with their fully qualified name.

Ex: Entity(name="p.c.k.Entity) & Entity(name="p.a.b.Entity)

But now I their corresponding daos are not able to autowire, and I am getting the following error:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type...

Do I have to change anything in Dao also to support this "name" attribute change in the entity.

I am using Hibernate, JPA and Spring.

Arjit
  • 147
  • 1
  • 2
  • 9

3 Answers3

0

I think you can use the @Qualifier annotation

@Autowired
@Qualifier("p.c.k.Entity")
private Entity entity;

Got from here

Community
  • 1
  • 1
Montolide
  • 773
  • 3
  • 12
  • 27
0

By default autowiring is done by type. So you can directly use @Autowired annotation as both Entity are different classes, make sure those are spring beans (here I mean those are managed by Spring).

@Autowired // nothing to specify, Spring automatically autowire the bean by checking type
private p.c.k.Entity entity;
@Autowired // nothing to specify, Spring automatically autowire the bean by checking type
private p.a.b.Entity entity1;
Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
0

I also had this problem, and couldn't find any way to work around it other than to rename one of the classes. Being in different packages should be enough, but it's not.

Ryan Shillington
  • 23,006
  • 14
  • 93
  • 108