javax.persistence.PersistenceException:无法找到持久性单元

时间:2015-07-08 23:03:43

标签: java hibernate maven jpa

我有一个Maven项目,我将它转换为JPA项目,现在可以与Maven一起使用。我的persistence.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://java.sun.com/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="my-pu" transaction-type="RESOURCE_LOCAL">
        <description>My Persistence Unit</description>
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>model.entity.Tweet</class>
        <class>model.entity.TweetHashtag</class>
        <class>model.entity.TweetUrl</class>
        <class>model.entity.User</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/tweetDB"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="root"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/tweetDB"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value=""/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        </properties>
    </persistence-unit>
</persistence>

我有以下实体:

package model.entity; 

@Entity
@Table(name="tweets")

public class Tweet implements Serializable{

    private static final long serialVersionUID = -1041037108182045708L;
    @Id
    @Column(name="tweet_id")
    private int tweetId;

    @Column(name="tweet_text")
    private String tweetText;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_at", nullable = false, length = 19)
    private Date createdAt;

    @Column(name="lang_code")
    private String languageCode;

    @ManyToOne(optional=false)
    @JoinColumn(name = "user_id",referencedColumnName="user_id")
    private User user;

    @OneToMany(mappedBy="tweet")
    private Set<TweetHashtag> hashtags=new HashSet<TweetHashtag>(0);

    @OneToMany(mappedBy="tweet")
    private Set<TweetUrl> links=new HashSet<TweetUrl>(0);

    public int getTweetId() {
        return tweetId;
    }

    public void setTweetId(int tweetId) {
        this.tweetId = tweetId;
    }

    public String getTweetText() {
        return tweetText;
    }

    public void setTweetText(String tweetText) {
        this.tweetText = tweetText;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public String getLanguageCode() {
        return languageCode;
    }

    public void setLanguageCode(String languageCode) {
        this.languageCode = languageCode;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Set<TweetHashtag> getHashtags() {
        return hashtags;
    }

    public void setHashtags(Set<TweetHashtag> hashtags) {
        this.hashtags = hashtags;
    }

    public Set<TweetUrl> getLinks() {
        return links;
    }

    public void setLinks(Set<TweetUrl> links) {
        this.links = links;
    }

}
//////////////////////////////////   
package model.entity;

@Entity
@Table(name="tweet_hashtag")
public class TweetHashtag implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 6302465277669302540L;

    @Id @GeneratedValue
    @Column(name="hashtag_id")
    private int hastagId;

    public int getHastagId() {
        return hastagId;
    }

    public void setHastagId(int hastagId) {
        this.hastagId = hastagId;
    }

    @ManyToOne
    @JoinColumn(name = "tweet_id",referencedColumnName="tweet_id")
    private Tweet tweet;

    @Column(name="hashtag")
    private String hashtag;

    public Tweet getTweet() {
        return tweet;
    }

    public void setTweet(Tweet tweet) {
        this.tweet = tweet;
    }

    public String getHashtag() {
        return hashtag;
    }

    public void setHashtag(String hashtag) {
        this.hashtag = hashtag;
    }

}

package model.entity;
///////////////////////////////////////

@Entity
@Table(name="tweet_urls")
public class TweetUrl implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = -606690240421717136L;

    @Id @GeneratedValue
    @Column(name="url_id")
    private int urlId;

    public int getUrlId() {
        return urlId;
    }

    public void setUrlId(int urlId) {
        this.urlId = urlId;
    }

    @ManyToOne
    @JoinColumn(name="tweet_id",referencedColumnName="tweet_id")
    private Tweet tweet;

    @Column(name="url")
    private String url;

    public Tweet getTweet() {
        return tweet;
    }

    public void setTweet(Tweet tweet) {
        this.tweet = tweet;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }



}
////////////////////////////
package model.entity;

@Entity
@Table(name="users")

public class User implements Serializable{

    @Id
    @Column(name="user_id")
    private int userId;

    @Column(name="screen_name")
    private String screenName;

    @Column(name="profile_image_url")
    private String profileImageUrl;

    @Column(name="statuses_count")
    private int statusesCount;

    @OneToMany(mappedBy="user")
    private List<Tweet> tweets;

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getScreenName() {
        return screenName;
    }

    public void setScreenName(String screenName) {
        this.screenName = screenName;
    }

    public String getProfileImageUrl() {
        return profileImageUrl;
    }

    public void setProfileImageUrl(String profileImageUrl) {
        this.profileImageUrl = profileImageUrl;
    }

    public int getStatusesCount() {
        return statusesCount;
    }

    public void setStatusesCount(int statusesCount) {
        this.statusesCount = statusesCount;
    }

    public List<Tweet> getTweets() {
        return tweets;
    }

    public void setTweets(List<Tweet> tweets) {
        this.tweets = tweets;
    }

}

//////////////////

这就是错误发生的地方:

package model;

public class TweetPersistent {

    private EntityManagerFactory emf;

    private EntityManager em;


    public void persist(Pair<String, QueryResult> pair)
    {
        List<Status> tweets = pair.getValue().getTweets();
        for (Status tweet : tweets) {
            this.insertToDB(pair.getKey(),tweet);
            // or  this.batchInsertToDB(pair.getKey(),tweet);

        }

    }

    private void insertToDB(String key, Status tweet) {
        emf=Persistence.createEntityManagerFactory("TwitterQueryApp");
        em=emf.createEntityManager();
        em.getTransaction().begin();
        em.persist(new Pair<String,Status>(key,tweet));
        em.getTransaction().commit();


// insert code here 

    }

    private void batchInsertToDB(String key, Status tweet) {
        // insert batch 

            }

}

///////////

现在错误:javax.persistence.PersistenceException:无法找到持久性单元

Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar 
log4j:WARN No appenders could be found for logger (twitter4j.HttpClientImpl).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "Thread-1" javax.persistence.PersistenceException: Unable to locate persistence units
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:101)
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:88)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:69)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at model.TweetPersistent.insertToDB(TweetPersistent.java:32)
    at model.TweetPersistent.persist(TweetPersistent.java:24)
    at controller.TwitterStreamConsumer.run(TwitterStreamConsumer.java:25)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.persistence.PersistenceException: Invalid persistence.xml.
Error parsing XML [line : -1, column : -1] : cvc-elt.1: Deklaration des Elements "persistence" kann nicht gefunden werden.
    at org.hibernate.jpa.boot.internal.PersistenceXmlParser.validate(PersistenceXmlParser.java:377)
    at org.hibernate.jpa.boot.internal.PersistenceXmlParser.loadUrl(PersistenceXmlParser.java:310)
    at org.hibernate.jpa.boot.internal.PersistenceXmlParser.parsePersistenceXml(PersistenceXmlParser.java:114)
    at org.hibernate.jpa.boot.internal.PersistenceXmlParser.doResolve(PersistenceXmlParser.java:104)
    at org.hibernate.jpa.boot.internal.PersistenceXmlParser.locatePersistenceUnits(PersistenceXmlParser.java:86)
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:97)
    ... 8 more

我在构建路径中包含了不同的jar:hibernate-validation,java-persistence,hibernate-jpa和hibernate-entityManager,以及项目本身的类路径。我还在资源文件夹中复制了persistence.xml。 我做了很多事情,但它不起作用,总是一样的错误。

提前感谢您的建议。

5 个答案:

答案 0 :(得分:2)

如果您使用Eclipse,请检查导出窗口&#39;将所需的库复制到生成的JAR旁边的子文件夹中。

答案 1 :(得分:1)

这看起来像jpa 2.1版本的xml架构有问题。

尝试并查看此解决方案,希望它适合您:How to specify JPA 2.1 in persistence.xml?

答案 2 :(得分:0)

您应该将持久性单元名称传递给Persistence.createEntityManagerFactory 你的persistence.xml中存在哪个:

<persistence-unit name="my-pu" transaction-type="RESOURCE_LOCAL"> 

尝试致电:

Persistence.createEntityManagerFactory("my-pu");

createEntityManagerFactory中作为参数给出的名称应存在于xml文件中的一个持久性元素中。

答案 3 :(得分:0)

在由Cargo Maven plugin管理的容器上运行的webapp中创建EntityManagerFactory时,例如嵌入式Tomcat8x容器,配置Cargo以便复制javax.transaction:jta和(隐式)org.hibernate:hibernate-entitymanager依赖于Tomcat的共享类路径,根据documentation

<project ...>
    :
    <dependencies>
        :
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>${jta.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate.ogm</groupId>
            <artifactId>hibernate-ogm-neo4j</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
    </dependencies>
    :
    <build>
        :
        <plugins>
            :
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>${cargo.version}</version>
                <configuration>
                    :
                    <container>
                        <type>embedded</type> 
                        <artifactInstaller>
                            <groupId>org.apache.tomcat</groupId>
                            <artifactId>tomcat</artifactId>
                            <version>${tomcat.version}</version>
                        </artifactInstaller>
                        :
                        <dependencies>
                            <dependency>
                                <groupId>org.hibernate</groupId>
                                <artifactId>hibernate-entitymanager</artifactId>
                                <classpath>shared</classpath>
                            </dependency>
                            <dependency>
                                <groupId>javax.transaction</groupId>
                                <artifactId>jta</artifactId>
                                <classpath>shared</classpath>
                            </dependency>
                        </dependencies>
                    </container>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

答案 4 :(得分:-1)

请问您可以尝试从xml中删除以下内容吗?

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