Saturday 15 March 2014

java - How to get all beans that have @Entity annotation? -



java - How to get all beans that have @Entity annotation? -

i trying bean annotation it's not working. wonder why ? here code snippet :

configurableapplicationcontext suggestorcontext = createcontext("context.xml"); // find @entities classes map beanswithannotation = suggestorcontext.getbeanswithannotation(entity.class);

my map size 0 !!!!

here context.xml file :

<?xml version="1.0" encoding="utf-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="ismds" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="com.mysql.jdbc.driver"/> <property name="username" value="user"/> <property name="password" value="password"/> <property name="url" value="jdbc:mysql://localhost:3306/mydb"/> </bean> <bean id="myemf" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean"> <property name="datasource" ref="ismds"/> <property name="jpavendoradapter"> <bean class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter"> <property name="showsql" value="false"/> <property name="databaseplatform" value="org.hibernate.dialect.mysql5innodbdialect"/> <property name="generateddl" value="false"/> </bean> </property> <property name="packagestoscan" value="com.myproject.entities.entity"/> </bean> <bean id="transactionmanager" class="org.springframework.orm.jpa.jpatransactionmanager"> <property name="entitymanagerfactory" ref="myemf"/> </bean> <tx:annotation-driven transaction-manager="transactionmanager"/> <bean class="org.springframework.dao.annotation.persistenceexceptiontranslationpostprocessor"/> <bean class="org.springframework.orm.jpa.support.persistenceannotationbeanpostprocessor"/> <jpa:repositories base-package="com.mypackage.dao" entity-manager-factory-ref="myemf"/> </beans>

all entities in next bundle : com.myproject.entities.entity

what wrong please ?

it not work, because spring's getbeanswithannotation(entity.class); homecoming spring beans, usual entities not spring beans, hence work.

maybe there way in jpa/hibernate obtain mapped classes (classes not entity instances!!).

an other ways find classes annotations discussed here: scanning java annotations @ runtime, in 1 of answers, lib called "google reflections" mentioned. used tool times ago , worked well.

java spring spring-data spring-annotations

No comments:

Post a Comment