Is the file persistence.xml required?

14,068

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.

Share:
14,068
Niklas Rosencrantz
Author by

Niklas Rosencrantz

I'm as simple as possible but not any simpler.

Updated on June 14, 2022

Comments

  • Niklas Rosencrantz
    Niklas Rosencrantz almost 2 years

    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>
    

    enter image description here