2015-07-20 55 views
-1

我想通过JPA @Entity注释一个类,所以我添加了Spring的@Component/@Resource/@Configurable注解,但它不允许我自动装入那个类。JPA @Entity和Spring之间的冲突@ Component/@ Resource/@ Configurable

是否需要其他注释?

样品是:

@Component 
@Entity 
@Table 
public class Employee{ 

    @Autowired 
    TestService testService; 
    ... 
} 

凡类TestService标注有@Service

+0

请向downvoted的人请求理由。 –

+1

我不是downvoter,但你为什么要这么做? Hibernate应该管理'@ Entity'对象的生命周期,而不是Spring,这是'@ Autowired'(和其他bean注释一起)的用处。 –

+0

我了解Predrag,但我有一个要求。 –

回答

1

您可以轻松地做到这一点,使一个构造,并通过注入的服务为perameter:

@Component 
@Entity 
@Table 
public class Employee{ 

    Employee(TestService testService){ 

     //Do some work  
    } 
    ... 
} 

现在主叫类。 。

@Service 
public class CallerClass{ 

    @Autowired 
    TestService testService; 

    Employee employee =new Employee(testService); 
}