jpa 是否需要文件persistence.xml?

rks48beu  于 2024-01-08  发布在  其他
关注(0)|答案(4)|浏览(117)

我的开发环境(IBM RAD 8 + WAS 8)抱怨我的项目没有persistence.xml文件。但似乎我可以构建和运行我的项目。该文件是必需的吗?如果添加一个这样的文件使我的项目通过验证,该文件中应该包含什么?
这个项目是一个Web项目,它使用了其他项目中的会话bean和实体bean,这个persistence.xml错误是这个项目中唯一的错误,所以我很乐意摆脱它。
谢谢你的帮助

更新

我在我的文件中搜索persistence.xml,它出现在EJB项目的src/和bin/中,而带有servlet和jsp的Web项目没有persistence.xml,根据我的同事的说法,Web项目正在使用EJB项目中的persistence.xml,即:

<?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>

字符串
x1c 0d1x的数据

xzv2uavs

xzv2uavs1#

persistence.xml文件通常包含与数据库相关的详细信息,例如连接字符串及其相应的用户名和密码,包括其他ORM相关信息。这些详细信息可以放置在其他位置,因此您不需要显式地拥有一个,尽管拥有这样的文件通常会使所有持久性相关信息在一个地方可用,这使得查找某些设置和配置更容易。
下面是一个persistence.xml示例文件:

<?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>

字符串
以上内容摘自here

moiiocjp

moiiocjp2#

<?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>

字符串

mqxuamgl

mqxuamgl3#

<?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>

字符串

qpgpyjmq

qpgpyjmq4#

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.time.Instant;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

class MongoLockProviderTest {

    @Mock
    private MongoDatabase mongoDatabase;

    @Mock
    private MongoCollection<Document> mongoCollection;

    @InjectMocks
    private MongoLockProvider mongoLockProvider;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.openMocks(this);
        when(mongoDatabase.getCollection(any())).thenReturn(mongoCollection);
        mongoLockProvider = new MongoLockProvider(mongoDatabase);
    }

    @Test
    void extendLock_Success() {
        LockConfiguration lockConfiguration = new LockConfiguration("lockName", Instant.now(), Instant.now().plusSeconds(60), "task");
        Document mockDocument = createMockDocument(lockConfiguration.getName(), lockConfiguration.getLockAtMostUntil());

        when(mongoCollection.findOneAndUpdate(any(), any())).thenReturn(mockDocument);

        Optional<SimpleLock> result = mongoLockProvider.extend(lockConfiguration);

        assertEquals(true, result.isPresent());
        assertEquals(lockConfiguration, result.get().getLockConfiguration());
    }

    @Test
    void extendLock_Unsuccessful() {
        LockConfiguration lockConfiguration = new LockConfiguration("lockName", Instant.now(), Instant.now().plusSeconds(60), "task");

        when(mongoCollection.findOneAndUpdate(any(), any())).thenReturn(null);

        Optional<SimpleLock> result = mongoLockProvider.extend(lockConfiguration);

        assertEquals(false, result.isPresent());
    }

    @Test
    void unlockLock_Success() {
        LockConfiguration lockConfiguration = new LockConfiguration("lockName", Instant.now(), Instant.now().plusSeconds(60), "task");

        mongoLockProvider.unlock(lockConfiguration);

        Mockito.verify(mongoCollection).findOneAndUpdate(eq(new Document("_id", "lockName")),
                any(Document.class));
    }

    // Add more test cases as needed...

    private Document createMockDocument(String lockName, Instant lockUntil) {
        Document document = new Document();
        document.put("_id", lockName);
        document.put("lockUntil", lockUntil.toEpochMilli());
        document.put("lockedBy", "hostname");
        return document;
    }
}

字符串

相关问题