gpt4 book ai didi

javax.persistence.PersistenceException : Unable to locate persistence units

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:44 27 4
gpt4 key购买 nike

我有一个 Maven 项目,我将其转换为现在可与 Maven 一起使用的 JPA 项目。我的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-validationjava-persistencehibernate-jpahibernate-entityManager 在项目本身的构建路径和类路径中。我还在资源文件夹中复制了 persistence.xml。我做了很多事情,但它不起作用,总是出现同样的错误。

提前感谢您的建议!

最佳答案

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

关于javax.persistence.PersistenceException : Unable to locate persistence units,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31305334/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com