开发者

Creating an EntityManagerFactory manually?

开发者 https://www.devze.com 2023-03-03 16:15 出处:网络
Does anyone know how to create an EntityManagerFactory manually? When I say manually I mean have it consume a special persistence.xml file?This what I have tried and all has failed me.

Does anyone know how to create an EntityManagerFactory manually? When I say manually I mean have it consume a special persistence.xml file? This what I have tried and all has failed me.

Configuration configuration = new Configuration();
InputStream is = JPAUtil.class.getResourceAsStream("/s-persistence.xml");
configuration.addInputStream(is);
_entityManagerFactory = Persistence.createEntityManagerFactory(
      puType.persistenceName, configuration.getProperties())开发者_如何学编程;

s-persistence.xml looking like:

<?xml version="1.0"?> 
<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="S_DATABASE" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property value="false" name="hibernate.show_sql"/>
            <property value="org.hibernate.dialect.OracleDialect" name="hibernate.dialect"/>
            <property value="oracle.jdbc.driver.OracleDriver" name="hibernate.connection.driver_class "/>
            <property value="jdbc:oracle:thin:@xxx.xxx.xxx.xxx:TEST" name="hibernate.connection.url"/>
            <property value="TESTOWNER" name="hibernate.connection.username"/>
            <property value="TEST" name="hibernate.connection.password"/>
            <property value="2" name="hibernate.connection.isolation"/>
        </properties>
    </persistence-unit>
</persistence>

I get the following error when running the java code.

ERROR   :: org.hibernate.util.XMLHelper|Error parsing XML: XML InputStream(2) Document     is invalid: no grammar found. 
ERROR   :: org.hibernate.util.XMLHelper|Error parsing XML: XML InputStream(2) Document root element "persistence", must match DOCTYPE root "null". 
org.hibernate.MappingException: invalid mapping

It's like it is looking for hibernate specific syntax in the persistence.xml. I've also tried the EJB3Configuration. It looks like my XML is well formed which is throwing me off as to why I am getting this error.


I think you are running into the issue that

http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd

may not be reachable, so that validating the XML file against it can not work.

Try to not give the schema location like this:

<persistence xmlns="http://java.sun.com/xml/ns/persistence">
0

精彩评论

暂无评论...
验证码 换一张
取 消