Spring: Method Injection Lookup How to use it?

Can I use Method Injection Lookup -- with a entity class?.I use Spring+JPA+Hibernate. This allow to inject a prototype bean into a singleton bean.Is this also possible with entity beans?A is prototype scoped bean.I want to put A(@Entity) into a class B (ex. DAO) with scope=singleton.Thanks

@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

你可以使用@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);
}
链接地址: http://www.djcxy.com/p/22692.html

上一篇: 使用参数和注入来实例化Spring原型bean

下一篇: Spring:方法注入查找如何使用它?