Simple H2 and Hibernate/JPA -
simple test h2 database, jpa , hibernate. gives no discerning error, not persist entity. sure missing extremely simple
persistence.xml in meta-inf/:
<?xml version="1.0" encoding="utf-8" ?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="thepersistenceunit" transaction-type="resource_local"> <provider>org.hibernate.ejb.hibernatepersistence</provider> <class>entities.person</class> <properties> <property name="connection.driver_class" value="org.h2.driver"/> <property name="hibernate.connection.url" value="jdbc:h2:./db/repository"/> <property name="hibernate.dialect" value="org.hibernate.dialect.h2dialect"/> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> <property name="hibernate.show_sql" value="true" /> </properties> </persistence-unit> the simple entity :
@entity public class person { @id @generatedvalue private integer id; private string firstname; public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } } the test:
public class testing { @test public void test2(){ entitymanagerfactory factory = persistence.createentitymanagerfactory("thepersistenceunit"); entitymanager themanager = factory.createentitymanager(); assertnotnull(themanager); person person = new person(); person.setfirstname("ana"); themanager.persist(person); system.out.println(person.getid()); person p = (person)themanager.find(person.class, 1); system.out.println(person.getid()); assertnotnull(p); } } the result :
aug 16, 2013 1:48:20 pm org.hibernate.annotations.common.version <clinit> info: hcann000001: hibernate commons annotations {4.0.1.final} aug 16, 2013 1:48:20 pm org.hibernate.version logversion info: hhh000412: hibernate core {4.0.1.final} aug 16, 2013 1:48:20 pm org.hibernate.cfg.environment <clinit> info: hhh000206: hibernate.properties not found aug 16, 2013 1:48:20 pm org.hibernate.cfg.environment buildbytecodeprovider info: hhh000021: bytecode provider name : javassist aug 16, 2013 1:48:21 pm org.hibernate.service.jdbc.connections.internal.drivermanagerconnectionproviderimpl configure info: hhh000402: using hibernate built-in connection pool (not production use!) aug 16, 2013 1:48:21 pm org.hibernate.service.jdbc.connections.internal.drivermanagerconnectionproviderimpl configure warn: hhh000148: no jdbc driver class specified property hibernate.connection.driver_class aug 16, 2013 1:48:21 pm org.hibernate.service.jdbc.connections.internal.drivermanagerconnectionproviderimpl configure info: hhh000115: hibernate connection pool size: 20 aug 16, 2013 1:48:21 pm org.hibernate.service.jdbc.connections.internal.drivermanagerconnectionproviderimpl configure info: hhh000006: autocommit mode: true aug 16, 2013 1:48:21 pm org.hibernate.service.jdbc.connections.internal.drivermanagerconnectionproviderimpl configure info: hhh000401: using driver [null] @ url [jdbc:h2:./db/repository] aug 16, 2013 1:48:21 pm org.hibernate.service.jdbc.connections.internal.drivermanagerconnectionproviderimpl configure info: hhh000046: connection properties: {autocommit=true, release_mode=auto} aug 16, 2013 1:48:21 pm org.hibernate.dialect.dialect <init> info: hhh000400: using dialect: org.hibernate.dialect.h2dialect aug 16, 2013 1:48:21 pm org.hibernate.engine.jdbc.internal.lobcreatorbuilder usecontextuallobcreation info: hhh000423: disabling contextual lob creation jdbc driver reported jdbc version [3] less 4 aug 16, 2013 1:48:21 pm org.hibernate.engine.transaction.internal.transactionfactoryinitiator initiateservice info: hhh000268: transaction strategy: org.hibernate.engine.transaction.internal.jdbc.jdbctransactionfactory aug 16, 2013 1:48:21 pm org.hibernate.hql.internal.ast.astquerytranslatorfactory <init> info: hhh000397: using astquerytranslatorfactory aug 16, 2013 1:48:21 pm org.hibernate.tool.hbm2ddl.schemaexport execute info: hhh000227: running hbm2ddl schema export aug 16, 2013 1:48:21 pm org.hibernate.tool.hbm2ddl.schemaexport execute info: hhh000230: schema export complete hibernate: drop table person if exists hibernate: create table person (id integer generated default identity, firstname varchar(255), lastname varchar(255), primary key (id)) null hibernate: select person0_.id id0_0_, person0_.firstname firstname0_0_, person0_.lastname lastname0_0_ person person0_ person0_.id=? null junit.framework.assertionfailederror @ test.testing.test2(testing.java:49) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39) the question: why doesn't persist instance/why doesn't throw error, etc ?
you trying persist record in database without opening transaction. not possible. should is:
entitymanager themanager = factory.createentitymanager(); themanager .gettransaction().begin(); person person = new person(); person.setfirstname("ana"); themanager.persist(person); themanager.gettransaction().commit();
Comments
Post a Comment