Why tables created in hibernate are deleted after stopping the server? - java

I run my website and it generates the tables in the database. I even get to register in the database at your site and give a select shows that it was registered. But if I stop the server, and view tables in mysql, the table disappeared.
The persistence looks like this:
<?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_2_0.xsd"
version="2.0">
<persistence-unit name="nome">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>br.com.nome.model.Tabela</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost/bd" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="senha" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>

Search for the string "hbm2ddl.auto" in your entire source code and see if the value of that property is set programmatically to "create-drop". That would result in the observed behavior.

Related

Hibernate: Auto-load classes marked as #Entity from jar from different classpath

I have application (.war) which uses entities from other jar file from different classpath. My persistance.xml looks like
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
<persistence-unit name="EAP" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/pscDataSource2</jta-data-source>
<class>com.myPackage.entity.MyEntity</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.jdbc.batch_size" value="50" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.generate_statistics" value="false" />
<property name="hibernate.id.new_generator_mappings" value="false" />
<property name="hibernate.ejb.interceptor" value="com.myPackage.interceptor.DelegatingInterceptor" />
</properties>
</persistence-unit>
</persistence>
With this xml I load MyEntity class successfully. However I would like to avoid listing all necessary classes in persistance.xml file. What I've found is <property name="hibernate.archive.autodetection" value="class, hbm" /> But with such approach I need to list jar files from where I want to load entities. So my persistance.xml file now looks like below and works fine
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
<persistence-unit name="EAP" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/pscDataSource2</jta-data-source>
<jar-file>lib/myJar.jar</jar-file> //new line
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.jdbc.batch_size" value="50" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.generate_statistics" value="false" />
<property name="hibernate.id.new_generator_mappings" value="false" />
<property name="hibernate.ejb.interceptor" value="com.myPackage.interceptor.DelegatingInterceptor" />
<property name="hibernate.archive.autodetection" value="class, hbm" /> //new line
</properties>
</persistence-unit>
</persistence>
Question: Is the any way to load entities from jars without listing them at all if I have dependencies on them from my application? I don't use Spring
i use this tag inside my persistence.xml and it worked for me. may be you should try it cause am using eclipselink implementation of JPA
<exclude-unlisted-classes>false</exclude-unlisted-classes>
According to Red Hat support empty <jar-file></jar-file> property is the solution. It works for me. So now my persistence.xml looks like
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
<persistence-unit name="EAP" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/pscDataSource2</jta-data-source>
<jar-file></jar-file> //fix
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.jdbc.batch_size" value="50" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.generate_statistics" value="false" />
<property name="hibernate.id.new_generator_mappings" value="false" />
<property name="hibernate.ejb.interceptor" value="com.myPackage.interceptor.DelegatingInterceptor" />
</properties>
</persistence-unit>
</persistence>

Hibernate turn off cache - Can't see changes made directly on database in app

I have simple problem. When I edit something directly in database for example by UPDATE statement I can't see any changes in my application. I read somewhere that this is reason of Hibernate cache or am wrong ?
My hibernate configuration looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
<persistence-unit name="transactionPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/TestTable?UseUnicode=true&characterEncoding=utf8" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="uberSecretUsername" />
<property name="hibernate.connection.password" value="uberSecretPassword" />
<property name="show_sql" value="true" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.connection.charSet" value="UTF-8" />
</properties>
</persistence-unit>
</persistence>
Its pretty basic nothing here that can surprise you. How can I disable this cache or how can I get this working. I just want simple see changes in app when I make changes directly on database

Is the file persistence.xml required?

My development environment (IBM RAD 8 + WAS 8) is complaining that my project does not have a persistence.xml file. Still it seems that I can build and run my project. Is that file required and if a add one such file to make my project pass validation, what should be in that file?
The project is a web project that uses session beans and entity beans from other projects and this persistence.xml error is the only error in the project so I'd be glad to get rid of it.
Thanks for any help
Update
I searched my files for persistence.xml and it showed up in src/ and bin/ of the EJB project while the web project with servlets and jsp does not have a persistence.xml, according to my colleague the web project is using the persistence.xml from the EJB project i.e:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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">
<persistence-unit name="PandoraArendeWeb" transaction-type="JTA">
<jta-data-source>jdbc/Mainframe_TEST_ADBUTV2</jta-data-source>
<class>se.prv.pandora.arendeprocess.entity.PRVNummer</class>
<class>se.prv.pandora.arendeprocess.entity.Ansokan</class>
<class>se.prv.pandora.arendeprocess.entity.NatAnsokan</class>
<class>se.prv.pandora.arendeprocess.entity.PctAnsokan</class>
<class>se.prv.pandora.arendeprocess.entity.ArendePerson</class>
<class>se.prv.pandora.arendeprocess.entity.Nyregistrering</class>
<class>se.prv.pandora.arendeprocess.entity.Anstalld</class>
<class>se.prv.pandora.arendeprocess.entity.Handlaggare</class>
<class>se.prv.pandora.arendeprocess.entity.OrgElement</class>
<class>se.prv.pandora.arendeprocess.entity.FysiskHandlaggare</class>
<class>se.prv.pandora.arendeprocess.entity.AnsvarigHandlaggare</class>
<class>se.prv.pandora.arendeprocess.entity.AnsvarigFysiskHandlaggare</class>
<class>se.prv.pandora.arendeprocess.entity.TeknikOmrade</class>
<class>se.prv.pandora.arendeprocess.entity.Person</class>
<class>se.prv.pandora.arendeprocess.entity.PRVNummerPerson</class>
<class>se.prv.pandora.arendeprocess.entity.Notering</class>
<class>se.prv.pandora.arendeprocess.entity.Lock</class>
<class>se.prv.pandora.arendeprocess.entity.LandKod</class>
<class>se.prv.pandora.arendeprocess.entity.ArbetsMomentLog</class>
<class>se.prv.pandora.arendeprocess.entity.SystemTypDel</class>
<class>se.prv.pandora.arendeprocess.entity.ArbetsMoment</class>
<class>se.prv.pandora.arendeprocess.entity.UnderStatus</class>
<class>se.prv.pandora.arendeprocess.entity.PatPers</class>
<class>se.prv.pandora.arendeprocess.entity.PrvLandP</class>
<class>se.prv.pandora.arendeprocess.entity.PkaPerln</class>
<class>se.prv.pandora.arendeprocess.entity.PctnPerl</class>
<class>se.prv.pandora.arendeprocess.entity.PersonToPatPersKoppl</class>
<class>se.prv.pandora.arendeprocess.entity.PRVNummerPersonKoppl</class>
<class>se.prv.pandora.arendeprocess.entity.Region</class>
<class>se.prv.pandora.arendeprocess.entity.Historik</class>
<class>se.prv.pandora.arendeprocess.entity.Egenskap</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
<!-- <persistence-unit name="PandoraArendeWeb_MSSQL" transaction-type="JTA">
<jta-data-source>jdbc/MSSQL_TEST_XA</jta-data-source>
<class>se.prv.pandora.arendeprocess.entity.PersonSearch</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
-->
</persistence>
persistence.xml files usually contain details related to your database, such as connection strings and their respective user names and passwords including other ORM related information. These details can be placed in other locations so you need not explicitly have one, although having such a file usually makes all persistence related information available in one place which makes looking up certain settings and configurations easier.
This is a sample persistence.xml file:
<?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="<PERSISTENCE UNIT NAME>">
<properties>
<!--
<property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
-->
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.password" value="<PASSWORD>"/>
<property name="hibernate.connection.url" value="jdbc:mysql://<HOST IP ADDRESS>/<DB NAME>"/>
<property name="hibernate.connection.username" value="<USERNAME>"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.c3p0.min_size" value="5"/>
<property name="hibernate.c3p0.max_size" value="20"/>
<property name="hibernate.c3p0.timeout" value="300"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.c3p0.idle_test_period" value="3000"/>
</properties>
</persistence-unit>
</persistence>
The above content was taken from here.
<?xml version="1.0" encoding="UTF-8" ?>
<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_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="AINS" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.tridenthyundai.ains.domainobject.AccessoriesDO</class>
<class>com.tridenthyundai.ains.domainobject.BranchDO</class>
<class>com.tridenthyundai.ains.domainobject.ContactDO</class>
<class>com.tridenthyundai.ains.domainobject.CustomerDO</class>
<class>com.tridenthyundai.ains.domainobject.FinanceDO</class>
<class>com.tridenthyundai.ains.domainobject.InsuranceDO</class>
<class>com.tridenthyundai.ains.domainobject.MessageDO</class>
<class>com.tridenthyundai.ains.domainobject.NotificationDO</class>
<class>com.tridenthyundai.ains.domainobject.ProductDO</class>
<class>com.tridenthyundai.ains.domainobject.ProductPriceDO</class>
<class>com.tridenthyundai.ains.domainobject.ProductSpecDO</class>
<class>com.tridenthyundai.ains.domainobject.ProductVariantDO</class>
<class>com.tridenthyundai.ains.domainobject.PurchaseDO</class>
<class>com.tridenthyundai.ains.domainobject.ServiceCentreDO</class>
<class>com.tridenthyundai.ains.domainobject.ServiceDO</class>
<class>com.tridenthyundai.ains.domainobject.ServiceTypeDO</class>
<class>com.tridenthyundai.ains.domainobject.UserDO</class>
<class>com.tridenthyundai.ains.domainobject.VisitorDO</class>
<!-- shouldn't be valid for java SE per specification, but it works for EclipseLink ... -->
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<!-- For Local Testing -->
<!-- <properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://saptalabs:3306/tridenthyundai" />
<property name="javax.persistence.jdbc.user" value="adminuser" />
<property name="javax.persistence.jdbc.password" value="adminuser" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="eclipselink.cache.shared.default" value="false"/>
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.logging.level" value="SEVERE" />
</properties> -->
<!-- For Production -->
</persistence-unit>
</persistence>
<?xml version="1.0" encoding="UTF-8" ?>
<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_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="saptalabs" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.sapta.hr.domainobject.UserDO</class>
<class>com.sapta.hr.domainobject.EmployeeDO</class>
<class>com.sapta.hr.domainobject.AddressDO</class>
<class>com.sapta.hr.domainobject.EmpDetailDO</class>
<class>com.sapta.hr.domainobject.EmpAccDetailDO</class>
<class>com.sapta.hr.domainobject.ProjectDO</class>
<class>com.sapta.hr.domainobject.CustomerDO</class>
<class>com.sapta.hr.domainobject.EmpAssignmentDO</class>
<class>com.sapta.hr.domainobject.EmpAboutDO</class>
<class>com.sapta.hr.domainobject.EmpAchievementsDO</class>
<class>com.sapta.hr.domainobject.EmpEmploymentHistoryDO</class>
<class>com.sapta.hr.domainobject.EmpSportsDO</class>
<class>com.sapta.hr.domainobject.EmpCulturalsDO</class>
<class>com.sapta.hr.domainobject.EmpEducationDO</class>
<class>com.sapta.hr.domainobject.EmpLanguageKnownDO</class>
<class>com.sapta.hr.domainobject.EmpReferencesDO</class>
<class>com.sapta.hr.domainobject.EmpSkillSetDO</class>
<class>com.sapta.hr.domainobject.EmpFamilyBackgroundDO</class>
<class>com.sapta.hr.domainobject.AssetDO</class>
<class>com.sapta.hr.domainobject.AssetTypeDO</class>
<class>com.sapta.hr.domainobject.EmpCTCDO</class>
<class>com.sapta.hr.domainobject.ExpenseDO</class>
<class>com.sapta.hr.domainobject.ExpTypeDO</class>
<class>com.sapta.hr.domainobject.InvoiceDO</class>
<class>com.sapta.hr.domainobject.PayrollDO</class>
<class>com.sapta.hr.domainobject.ProfessionalTaxDO</class>
<class>com.sapta.hr.domainobject.TDSDO</class>
<class>com.sapta.hr.domainobject.VendorDO</class>
<class>com.sapta.hr.domainobject.BillsDO</class>
<class>com.sapta.hr.domainobject.EmpLoseOfPayDO</class>
<!-- shouldn't be valid for java SE per specification, but it works for
EclipseLink ... -->
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<!-- For Local Testing -->
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hrportal" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="eclipselink.cache.shared.default" value="false" />
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<property name="eclipselink.logging.level" value="SEVERE" />
</properties>
</persistence-unit>
</persistence>

No Persistence provider for EntityManager named with persistence file in correct spot

I've been searching around and haven't been able to find a working fix. My persistence.xml file is located in /src/META-INF/persistence.xml, which from looking around, this is the correct location for it.
I'm using glassfish as the server, and I keep getting the following:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named pers
Here is my persistence file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
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">
<persistence-unit name="pers" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>cs.ee.assignment2.Client</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.archive.autodetection" value="class, hbm" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/assignment2" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
Any ideas on what the problem could be would be greatly appreciated.
Hmm.. make sure you are using PeristenceUnit not context in your entity class: see http://openejb.apache.org/jpa-concepts.html
Switch to "transaction" just to see if it fails too.
Also sometimes the old classes are not unloaded from the server properly if you are doing redeployment, you may have to shut it down and restart after redeploy.

"Unknown database 'mydb' " error with jpa

i'm trying to connect to mysql server but get this error. when mydb is the only name in the system. please tell me where i'm wrong...
my persistence xml is as folow:
<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="MyPersistenceUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mydb" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="tft" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.use_sql_comments" value="true" />
</properties>
</persistence-unit>
</persistence>
1.You have to check your mysql jdbc driver
2.You have to check mysql server state and validation of username & password.

Categories

Resources