Maven和Hibernate JPA项目错误

时间:2018-05-09 06:40:08

标签: java hibernate maven jpa

当我在Apache Maven中构建Hibernate JPA映射时,我遇到了一个错误。

Description Resource Path Location Type
The persistence.xml file does not have supported content for this JPA 
platform.   persistence.xml /LearningManagementService/src/main/resources/META-INF JPA Problem

我的src / main / java有一个com.ph.deped.model

User.java

package com.ph.deped.model;


/**
* User generated by hbm2java
*/
 public class User implements java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = -2357935130826100477L;
private long id;
private Privilege privilege;
private String username;
private String password;

public User() {
}

public User(Privilege privilege, String username, String password) {
    this.privilege = privilege;
    this.username = username;
    this.password = password;
}

public long getId() {
    return this.id;
}

public void setId(long id) {
    this.id = id;
}

public Privilege getPrivilege() {
    return this.privilege;
}

public void setPrivilege(Privilege privilege) {
    this.privilege = privilege;
}

public String getUsername() {
    return this.username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return this.password;
}

public void setPassword(String password) {
    this.password = password;
}

}

Privilege.java

package com.ph.deped.model;
// Generated May 7, 2018 9:04:03 PM by Hibernate Tools 5.2.3.Final

import java.util.HashSet;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * Privilege generated by hbm2java
 */

public class Privilege implements java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = 910980850904284147L;
private long id;
private String description;

@JsonIgnore
private Set<User> users = new HashSet<User>(0);

public Privilege() {
}

public Privilege(String description, Set<User> users) {
    this.description = description;
    this.users = users;
}

public long getId() {
    return this.id;
}

public void setId(long id) {
    this.id = id;
}

public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

public Set<User> getUsers() {
    return this.users;
}

public void setUsers(Set<User> users) {
    this.users = users;
}

}

然后我的hbm.xml映射位于src / main / resources中,其中com.ph.deped.model包与实体对象相同

User.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true" default-access="property" default- 
cascade="none" default-lazy="true">
<class catalog="elearning" dynamic-insert="false" dynamic-update="false" mutable="true" name="com.ph.deped.model.User" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="user">
    <id name="id" type="long">
        <column name="id"/>
        <generator class="identity"/>
    </id>
    <many-to-one class="Privilege" embed-xml="true" fetch="select" insert="true" name="privilege" not-found="exception" optimistic-lock="true" unique="false" update="true">
        <column name="privilege_id"/>
    </many-to-one>
    <property generated="never" lazy="false" name="username" optimistic-lock="true" type="string" unique="false">
        <column name="username"/>
    </property>
    <property generated="never" lazy="false" name="password" optimistic-lock="true" type="string" unique="false">
        <column name="password"/>
    </property>
</class>
</hibernate-mapping>

Privilege.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true" default-access="property" default- 
cascade="none" default-lazy="true">
<class catalog="elearning" dynamic-insert="false" dynamic-update="false" mutable="true" name="com.ph.deped.model.Privilege" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="privilege">
    <id name="id" type="long">
        <column name="id"/>
        <generator class="identity"/>
    </id>
    <property generated="never" lazy="false" name="description" optimistic-lock="true" type="string" unique="false">
        <column name="description"/>
    </property>
    <set embed-xml="true" fetch="select" inverse="true" lazy="true" mutable="true" name="users" optimistic-lock="true" sort="unsorted" table="user">
        <key on-delete="noaction">
            <column name="privilege_id"/>
        </key>
        <one-to-many class="User" embed-xml="true" not-found="exception"/>
    </set>
</class>
</hibernate-mapping>

最后我的persistence.xml位于src / main / resources下的META-INF / persistence.xml

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>

<persistence 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_1_1.xsd" 
version="1.1">

<persistence-unit name="Elearning" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <mapping-file>/com/ph/deped/model/UserLogin.hbm.xml</mapping-file>
    <mapping-file>/com/ph/deped/model/Privilege.hbm.xml</mapping-file>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value="1a0b1c6d0d6c1b3aA@"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        <property name="hibernate.enable_lazy_load_no_trans" value="true"/>
        <property name="hibernate.show_sql" value="false"/>
    </properties>
</persistence-unit>

</persistence>

0 个答案:

没有答案