Spring:Method Injection Lookup如何使用它?

时间:2014-12-02 09:13:36

标签: spring spring-mvc

我可以使用方法注入查找 - 使用实体类吗?。我使用Spring + JPA + Hibernate。这允许将一个原型bean注入一个单独的bean。这也可以用实体bean吗?A是原型范围的bean。我想把A(@Entity)放到一个带有scope = singleton.Thanks的B类(例如DAO)中

@Entity
public class A(){

    private String name;
    private String surname;

    ...//get and set
}//A

public interface DAO{
   public void method();
}//DAO

public class DAOImpl implements DAO{
  private A object_a;

  public void method(){
     //In this method I use everytime a new istance of A
  }//method
}//DAOImpl

1 个答案:

答案 0 :(得分:0)

您可以使用@Embedded包含您的子bean,并在您的sql中使用。

@Entity
public class User(){

    private String name;

    @Embedded
    private Address address;

    @Bean(scope=DefaultScopes.PROTOTYPE)
    public User() {
    }

    ...//get and set
}


@Entity
public class Address(){
    private String name;
    ...//get and set
}

public interface UserRepository extends JpaRepository<User, Long> {
    @Query(value = "select u from users u where u.address.name = :addressName")
    List<Blog> findUserByAddress(@Param("addressName") String addressName);
}