Idea doesn't recognise persistence.xml - java

I faced with some weird behavior: my IDE can't recognize xml document if it named "persistence.xml" and all features like syntax coloring & code completion doesn't work for this file.
But when I rename it, idea start to support this xml document.
So, what can be the reason of this behavior?
(Debian, IDEA 14)
Updated
persistence.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="ex01" transaction-type="JTA">
<jta-data-source>java:global/jdbc/ch08</jta-data-source>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="javax.persistence.schema-generation-target" value="database-and-scripts"/>
<property name="javax.persistence.schema-generation.scripts.action" value="create"/>
<property name="javax.persistence.schema-generation.scripts.create-target" value="create.sql"/>
<property name="javax.persistence.sql-load-script-source" value="sql/initData.sql"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:07DB;create=true"/>
<property name="eclipselink.logging.level" value="OFF"/>
</properties>
</persistence-unit>
</persistence>

Related

Connect JPA eclipselink to mlab

Hi guys I have a big problem: I developed my webservice with eclipselink JPA + mongoDb, everything works fine on my local machine but when I try to set the persistence.xml for the connection to a remote NoSql Database it doesn't work.
Here's my persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="introsde-final-jpa">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.target-database" value="org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform"/>
<property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec"/>
<property name="eclipselink.nosql.property.mongo.host" value=<my_host>/>
<property name="eclipselink.nosql.property.mongo.port" value=<my_port>/>
<property name="eclipselink.nosql.property.mongo.db" value="mydb"/>
<property name="eclipselink.nosql.property.mongo.user" value=<my_user>/>
<property name="eclipselink.nosql.property.mongo.password" value=<my_password>/>
</properties>
</persistence-unit>
</persistence>
any suggestion is appreciated!!

How to populate a DB with my JPA entities in IntelliJ

Is there a way in IntelliJ to create tables in a database from my entity classes? I mean without ER diagram. The IDE allows me to create entities from the DB, but not the other way around.
I used to use netbeans and it just lets you add entities to your persistense.xml and select mode (drop and create, create, etc), but I can't find these options in IntelliJ.
I have configures de DB connection and it works, it just doesn't allow me to populate it with my entities.
Here's my persistence.xml from NetBeans (works perfect)
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="ClubPU2" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>baseclub.entidades.Actividad</class>
<class>baseclub.entidades.Comercio</class>
<class>baseclub.entidades.Contacto</class>
<class>baseclub.entidades.Cuota</class>
<class>baseclub.entidades.DatosInstitucionales</class>
<class>baseclub.entidades.DatosPersonales</class>
<class>baseclub.entidades.Descuento</class>
<class>baseclub.entidades.Identificacion</class>
<class>baseclub.entidades.Miembro</class>
<class>baseclub.entidades.NotaEntrada</class>
<class>baseclub.entidades.Participacion</class>
<class>baseclub.entidades.Rol</class>
<class>baseclub.entidades.Secretaria</class>
<class>baseclub.entidades.Sesion</class>
<class>baseclub.entidades.Tutor</class>
<class>baseclub.entidades.Ubicacion</class>
<class>baseclub.entidades.Usuario</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/club_universitario?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.password" value="XXXXXX"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="club"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
And here's persistence.xml from IntelliJ:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="persistenceUnit">
</persistence-unit>
</persistence>
Is there any particular reason you want IntelliJ to create the database?
if you have selected Hibernate as ORM , you can simply add in your persistence.xml the following code:
<property name="hibernate.hbm2ddl.auto" value="update" />
Start your application server, deploy your app and the database will be created.

JPA removes MySql triggers

Because my persistence.xml uses drop-and-create-tables:
<?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="USENETPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>net.bounceme.dur.usenet.model.NewsgroupsArticles</class>
<class>net.bounceme.dur.usenet.model.Article</class>
<class>net.bounceme.dur.usenet.model.Newsgroup</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/nntp"/>
<property name="javax.persistence.jdbc.password" value="password"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="java"/>
<property name="toplink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
My triggers get removed each run. Where can I put the triggers so that they're not removed?
You should use a SessionCustomizer. And within your persistence properties you should insert a property pointing your SessionCustomizer implementation.
properties.put(PersistenceUnitProperties.SESSION_CUSTOMIZER, MySessionCustomizer.class.getName());
or
<property name="eclipselink.session.customizer" value="foo.bar.MySessionCustomizer" />
and within this session customizer you could load your trigger codes and let it be executed with:
unitOfWork.executeNonSelectingSQL(sql);
A detailed explanation regarding this topic you can find here

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>

ClassCastException with jetty and jpa

When I try to execute my application based on jersey, guice, and jpa on jetty (7.2.0 or 8.0.0.M1) I get the following error, even if I don't use JNDI.
java.lang.ClassCastException: org.eclipse.jetty.jndi.NamingContext cannot be cast to javax.sql.DataSource
Caused by:
javax.persistence.PersistenceException: java.lang.ClassCastException: org.eclipse.jetty.jndi.NamingContext cannot be cast to javax.sql.DataSource
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:397)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:151)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:207)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:195)
...
Here is the corresponding persistence.xml
<?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="myproject" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:mydb;create=true"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
<non-jta-data-source/>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
</persistence-unit>
</persistence>
Any ideas?
You don't need <non-jta-data-source/> since you don't use JNDI.

Categories

Resources