I am new to Hibernate and am wondering whether there is any way to use it without the xml configuration file.
As far as I understood you can replace the mapping by using annotations, but is there a way, to declare the entities with the EntityManager programmatically?
I tried to modify the hellojpa example, but I always get an exception:
Code:
Exception in thread "main" java.lang.IllegalArgumentException: Unknown entity: hello.Message
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:215)
at hello.HelloWorld.main(HelloWorld.java:30)
Code:
System.getProperties().put("hibernate.connection.driver_class","org.apache.derby.jdbc.EmbeddedDriver");
System.getProperties().put("hibernate.connection.url","jdbc:derby:c:/tmp/database;create=true");
System.getProperties().put("hibernate.connection.username","");
System.getProperties().put("hibernate.c3p0.min_size","5");
System.getProperties().put("hibernate.c3p0.max_size","30");
System.getProperties().put("hibernate.c3p0.timeout","300");
System.getProperties().put("hibernate.c3p0.max_statements","50");
System.getProperties().put("hibernate.c3p0.idle_test_period","3000");
System.getProperties().put("hibernate.dialect","org.hibernate.dialect.DerbyDialect");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("helloworld");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Message message = new Message("Hello World with JPA");
em.persist(message);