eclipse - Persisting entities -
i getting started ejb development, created test system consists of following 3 projects:
actually, helloworldbeanremote
interface exist in ejbtest
project. haven't found way import other project without runtime exceptions.
the ejbtestinterfaces
plain java project contains remote interface. ejbtest
contains program logic. helloworldbean
session bean. constructor sets created
field current time. in sayhello()
method uses injected persistencemanager
retrieve testentity
id 0 (or creates if not exist), increments ´hit` variable , returns it:
@persistencecontext(name="manager1") private entitymanager em; @override public string sayhello() { string info; if (em == null) info = "entity manager null"; else { testentity entity; try { entity = em.find(testentity.class, 0); entity.sethits(entity.gethits() + 1); em.merge(entity); info = "hit entity " + entity.gethits() + " times."; } catch(exception x) { entity = new testentity(); em.persist(entity); info = "never used entity bean before."; } } return "hello! created @ " + created.tostring() + "<br>" + info; }
the persistence unit defined in persistence.xml follows:
<persistence> <persistence-unit name="manager1"> <jta-data-source>java:jboss/datasources/appointmentds</jta-data-source> <jar-file>../ejbtest.jar</jar-file> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.hsqldialect"/> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> <!-- tried value="validate" --> </properties> </persistence-unit> </persistence>
it uses embedded database defined in standalone.xml:
<datasource jndi-name="java:jboss/datasources/appointmentds" pool-name="appointmentds" enabled="true" use-java-context="true"> <connection-url>jdbc:h2:file:[path file]</connection-url> <driver>h2</driver> <security> <user-name>sa</user-name> <password>sa</password> </security> </datasource>
the servlet outputs return value of sayhello()
:
doget(...) { //get initial context ... bean = (helloworldbeanremote)initialcontext.lookup(name); output.write(bean.sayhello()); }
if call servlet via web browser, expected output: creation date , "never used entity bean before." if refresh page, creation date not change, hit count increments. can restart servlet project without changing behaviour. hit count increments steadily.
however, if restart ejb project, reset zero. expected behaviour creation date, hit count should read database. not.
i can see created database files in specified directory , seem contain data (i opened file in text editor).
am supposed use session bean way did? not sure if have close bean after request (so transaction can commit).
how can make ejb project read persisted data database file?
you need change hibernate.hbm2ddl.auto
value create-drop
validate
or other value. create-drop
delete whole schema when sessionfactory closed , recreate again on open.
Comments
Post a Comment