0

Does Hibernate (JPA) have any built in mechanism to scan for @Entity annotated beans at start-up. I am trying to avoid having to maintain specific 'hbm' files for each persistent object.

Right now I have the following:

Test.java

@Entity
public class Test {
 ...
}

Test.hbm.xml

<hibernate-mapping package="domain">
    <class name="Test" table="test">
    ...
    </class>
</hibernate-mapping>

1 Answers1

0

You can create all @Entity beans for each hbm file and then you can drop all hbm files as hibernate JPA will map each bean with specific table at startup time

Yes, it will map at statup time, dont worry.

You can do something like this

<persistence-unit name="myPetStorePU">
    <description>Petstore Persistence Unit</description>
    <provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>

  <class>com.sun.javaee.blueprints.petstore.model.Tag</class>
  <class>com.sun.javaee.blueprints.petstore.model.SellerContactInfo</class>
  <class>com.sun.javaee.blueprints.petstore.model.Product</class>
  <class>com.sun.javaee.blueprints.petstore.model.Item</class>
  <class>com.sun.javaee.blueprints.petstore.model.Category</class>
  <class>com.sun.javaee.blueprints.petstore.model.Address</class>
  <class>com.sun.javaee.blueprints.petstore.model.ZipLocation</class>
    <properties>
        <property name="toplink.jdbc.driver" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="toplink.jdbc.url" value="jdbc:oracle:thin:@#############"/>
        <property name="toplink.jdbc.user" value="####"/>
        <property name="toplink.jdbc.password" value="#####"/>
        <property name="toplink.logging.level" value="INFO"/>
    </properties>

</persistence-unit>

Where all models are the @Entity classes.

Manoj Kathiriya
  • 3,366
  • 5
  • 19
  • 19
  • Thanks for the suggestion, however this is not working. Without the specific hbm files getting this error: Unknown entity: Test – user1665889 Feb 12 '13 at 13:39
  • but now you have to change your configuration files, instead of loading hbm.xml files, now you have to use context:component scan – Manoj Kathiriya Feb 12 '13 at 13:43
  • nop at all, when you are creating EntityManager factory in that configuration you have to pass all entity classes instead of hbm files. – Manoj Kathiriya Feb 12 '13 at 13:50
  • have a look this link http://stackoverflow.com/questions/4381724/persistenceunit-annotation-wont-create-an-entitymanagefactory-emf-null – Manoj Kathiriya Feb 12 '13 at 13:57
  • Thanks again, however I am looking for a solution where I do not need to maintain a separate entity (ie persistance.xml). Looking for a scanner that runs at startup that collects all classes annotated with @Entity. I will build something custom if that does not already exist. – user1665889 Feb 12 '13 at 17:47