jpa的持久化文件persistence.xml应该放在哪儿

piaoranxinyu 2015-02-04 11:02:33

建了个java项目,
dao:

public class NewsDao {

private static final EntityManagerFactory emf =
Persistence.createEntityManagerFactory("qs");

public static void main(String[] args) {

final EntityManager em = emf.createEntityManager();

News news = new News();
news.setTitle("测试title");
em.persist(news);

}

}

实体:

@Entity
@Table(name="news_table")
public class News {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;

@Column(name="news_title",length=50)
private String title;

public int getId() {
return id;
}

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

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

}

配置文件:persistence.xm

<?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 persistence_1_0.xsd">

<persistence-unit name="qs" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.wjf.entities.News</class>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<!--
<property name="hibernate.connection.url" value="jdbc.mysql://localhost:3306/testdatabase?useUnicode=true&characterEncoding=UTF-8" />
-->
<property name="hibernate.connection.url" value="jdbc.mysql://localhost:3306/world" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="123456" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>

</persistence-unit>

</persistence>


运行的时候报错:
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named qs;
且弹出框:
Could not find the main class:com.wjf.dao.NewsDao
是不是文件配置文件放错位置了,我放在src类路径下?
...全文
1847 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Allen_Chao 2015-02-04
  • 打赏
  • 举报
回复
那这个恐怕不好办了,你至少得告诉jpa你的配置文件在什么位置
piaoranxinyu 2015-02-04
  • 打赏
  • 举报
回复
引用 2 楼 Allen_Chao 的回复:
还有把你的 application-context.xml 贴出来看看
我只是建了个java项目,没有application-context.xml
Allen_Chao 2015-02-04
  • 打赏
  • 举报
回复
还有把你的 application-context.xml 贴出来看看
Allen_Chao 2015-02-04
  • 打赏
  • 举报
回复
<property name="persistenceXmlLocation" value="classpath:/META-INF/persistence.xml" /> 放到资源根目录下:新建文件夹 META-INF 貌似必须是这个目录,不然启动的过程jpa会提示让你存放到以 META-INF 命名的目录下的
piaoranxinyu 2015-02-04
  • 打赏
  • 举报
回复
引用 4 楼 Allen_Chao 的回复:
那这个恐怕不好办了,你至少得告诉jpa你的配置文件在什么位置
兄台来探讨一番撒,jdbc连接没问题,因为我用jdbc连数据库然后也查出其他表内容来了
piaoranxinyu 2015-02-04
  • 打赏
  • 举报
回复
引用 5 楼 piaoranxinyu 的回复:
[quote=引用 4 楼 Allen_Chao 的回复:] 那这个恐怕不好办了,你至少得告诉jpa你的配置文件在什么位置
我在src目录下建了个 META-INF目录,控制台运行不报错了,应该读取到文件了,但是没有在数据库中建表,不知道哪儿错了[/quote] 看我的配置文件:

<?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 persistence_1_0.xsd">

	<persistence-unit name="qs" transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<class>com.wjf.entities.News</class>
		<properties>  
		    <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
		    <!-- 
		    <property name="hibernate.connection.url" value="jdbc.mysql://localhost:3306/testdatabase?useUnicode=true&characterEncoding=UTF-8" />
		     --> 
		    <property name="hibernate.connection.url" value="jdbc.mysql://localhost:3306/world" />  
		    <property name="hibernate.connection.username" value="root" />  
		    <property name="hibernate.connection.password" value="123456" />
		    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> 
		    <property name="hibernate.show_sql" value="true" />  
		    <property name="hibernate.hbm2ddl.auto" value="update"/>  
   		</properties>  
	
	</persistence-unit>

</persistence>
测试运行通过,但是数据库中没有建表?

public class NewsTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		
	}
	
	@Test
	public void createTable(){
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("qs");
		emf.close();
	}

}
piaoranxinyu 2015-02-04
  • 打赏
  • 举报
回复
引用 4 楼 Allen_Chao 的回复:
那这个恐怕不好办了,你至少得告诉jpa你的配置文件在什么位置
我在src目录下建了个 META-INF目录,控制台运行不报错了,应该读取到文件了,但是没有在数据库中建表,不知道哪儿错了

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧