Announcement Announcement Module
Collapse
No announcement yet.
Spring Data without Autowiring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Data without Autowiring

    Is it possible to use Spring Data **without** using @Autowired Repositories? I've been googling for such an example with no luck.

    I see a statement in the Spring Data documentation that you may manually define a repository:

    Code:
    <repositories base-package="com.acme.repositories">
      <repository id="userRepository" />
    </repositories>
    However, when I do this, I am not able to reference bean id="userRepository" elsewhere in my config xml.

    Am thinking I might have to just use JPATemplate....

    Chris

  • #2
    Hi, yes, it's possible, for example, if you have the repository "com.acme.repositories.UserRepository", you can write (data-jpa.xml or similar):

    Code:
    	<jpa:repositories base-package="com.acme.repositories" />
    	<bean id="myService"
    		class="com.acme.service.MyServiceImpl">
    		<property name="userRepository" ref="userRepository"></property>
    	</bean>

    Comment


    • #3
      I am able to get it to work if I create my own repo:

      Repo declaration:

      Code:
          <bean id="tradeRepositoryCustom" class="com.tradeflow.dataaccess.repository.TradeRepositoryCustomImpl">
              <constructor-arg name="domainClass"  type="java.lang.Class" value="com.tradeflow.dataaccess.domain.TradeHeader"/>
              <constructor-arg name="emf" ref="entityManagerFactory"/>
          </bean>
      Repo code:

      Code:
      public class TradeRepositoryCustomImpl extends SimpleJpaRepository<TradeHeader, Integer> implements ITradeRepositoryCustom {
      
          public TradeRepositoryCustomImpl(Class<TradeHeader> domainClass, EntityManagerFactory emf) {
              super(domainClass, emf.createEntityManager());
          }
      
          @Override
          public List<TradeHeader> findByUpdateTimeGreaterThan(Timestamp updateTime) {
              return null;  //To change body of implemented methods use File | Settings | File Templates.
          }
      }

      What I was trying to do was avoid manually creating the repo, such that I could use the Spring Data created queries using method names ( as descibed here: http://static.springsource.org/sprin...ery-creation):

      Code:
      public interface ITradeRepositoryCustom extends CrudRepository<TradeHeader, Integer> {
      
          List<TradeHeader> findByUpdateTimeGreaterThan(Timestamp updateTime);
      
      }
      Seems like without using annotation scanning, I have to manually create these queries...

      -Chris

      Comment


      • #4
        OK, you can do this, in the example, com.acme.repositories.UserRepository can be the interface:

        Code:
        public interface UserRepository extends CrudRepository<User, UserPK>, JpaSpecificationExecutor<User> {
        	List<User> findByApplication(Application application); 
        }

        Comment


        • #5
          This worked great, venosov!!! Thanks!!!

          Comment


          • #6
            Hi, yes, it's possible, for example, if you have the repository "com.acme.repositories.

            Comment

            Working...
            X