问题描述
2012-11-2021:42:32org.hibernate.annotations.common.Version<clinit>INFO:HCANN000001:HibernateCommonsAnnotations{4.0.1.Final}2012-11-2021:42:32org.hibernate.VersionlogVersionINFO:HHH000412:HibernateCore{4.1.2.Final}2012-11-2021:42:32org.hibernate.cfg.Environment<clinit>INFO:HHH000206:hibernate.propertiesnotfound2012-11-2021:42:32org.hibernate.cfg.EnvironmentbuildBytecodeProviderINFO:HHH000021:Bytecodeprovidername:javassist2012-11-2021:42:32org.hibernate.ejb.Ejb3ConfigurationconfigureINFO:HHH000318:CouldnotfindanyMETA-INF/persistence.xmlfileintheclasspath2012-11-2021:42:32org.hibernate.ejb.Ejb3ConfigurationconfigureINFO:HHH000318:CouldnotfindanyMETA-INF/persistence.xmlfileintheclasspath2012-11-2021:42:32org.hibernate.ejb.Ejb3ConfigurationconfigureINFO:HHH000318:CouldnotfindanyMETA-INF/persistence.xmlfileintheclasspath2012-11-2021:42:32org.hibernate.ejb.Ejb3ConfigurationconfigureINFO:HHH000318:CouldnotfindanyMETA-INF/persistence.xmlfileintheclasspath不知道这哥错在哪里,本人刚学习JPA,不是太懂,恳请高手指教1<?xmlversion="1.0"encoding="UTF-8"?>2<persistenceversion="2.0"xmlns="http://java.sun.com/xml/ns/persistence"3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"4xsi:schemaLocation="http://java.sun.com/xml/ns/persistence5http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">67<!--为持久化单元取名为myJPA-->8<persistence-unitname="myJPA"transaction-type="RESOURCE_LOCAL">9<properties>10<!--配置Hibernate方言-->11<propertyname="hibernate.dialect"value="org.hibernate.dialect.MySQL5Dialect"/>12<!--配置数据库驱动-->13<propertyname="hibernate.connection.driver_class"value="com.mysql.jdbc.Driver"/>14<!--配置数据库用户名-->15<propertyname="hibernate.connection.username"value="root"/>16<!--配置数据库密码-->17<propertyname="hibernate.connection.password"value="root"/>18<!--配置数据库url-->19<propertyname="hibernate.connection.url"value="jdbc:mysql://localhost:3306/jpa?useUnicode=true&characterEncoding=UTF-8"/>20<!--设置外连接抓取树的最大深度-->21<propertyname="hibernate.max_fetch_depth"value="3"/>22<!--自动输出schema创建DDL语句-->23<propertyname="hibernate.hbm2ddl.auto"value="update"/>24</properties>25</persistence-unit>2627</persistence>实体类如下:1packagecom.cndatacom.jpa.entity;23importjavax.persistence.Column;4importjavax.persistence.Entity;5importjavax.persistence.GeneratedValue;6importjavax.persistence.Id;7importjavax.persistence.Table;89@Entity10@Table(name="t_user")11publicclassUser{1213/**14*主键15*/16@Id17@GeneratedValue18privateLongid;1920/**21*名字22*/23@Column(name="name")24privateStringname;2526/**27*密码28*/29@Column(name="password")30privateStringpassword;3132publicLonggetId(){33returnid;34}3536publicvoidsetId(Longid){37this.id=id;38}3940publicStringgetName(){41returnname;42}4344publicvoidsetName(Stringname){45this.name=name;46}4748publicStringgetPassword(){49returnpassword;50}5152publicvoidsetPassword(Stringpassword){53this.password=password;54}5556}测试类如此:1packagecom.cndatacom.jpa.test;23importjavax.persistence.EntityManager;4importjavax.persistence.EntityManagerFactory;5importjavax.persistence.Persistence;67importorg.junit.After;8importorg.junit.Before;9importorg.junit.Test;1011importcom.cndatacom.jpa.entity.User;121314publicclassTestJPA{1516EntityManagerFactoryemf=null;1718@Before19publicvoidbefore(){20//根据在persistence.xml中配置的persistence-unitname创建EntityManagerFactory21emf=Persistence.createEntityManagerFactory("myJPA");22}2324/**25*添加用户26*/27@Test28publicvoidaddUser(){2930//创建一个用户31Useruser=newUser();32user.setName("叶开");33user.setPassword("yekai");3435//创建实体管理器对象36EntityManagerem=emf.createEntityManager();37//开启事务38em.getTransaction().begin();39//持久化对象40em.persist(user);41//提交事务42em.getTransaction().commit();43//关闭EntityManager44em.close();45}4647/**48*修改用户(用户处于托管状态)49*/50@Test51publicvoidmodifyUser1(){52EntityManagerem=emf.createEntityManager();53em.getTransaction().begin();54//查找id为1的User,进行更新55Useruser=em.find(User.class,1L);//User的主键id为Long型56user.setName("楚留香");57em.getTransaction().commit();58em.close();59}6061/**62*修改用户(用户处于游离(脱管)状态)63*/64@Test65publicvoidmodifyUser2(){66EntityManagerem=emf.createEntityManager();67em.getTransaction().begin();68//查找id为1的User,进行更新69Useruser=em.find(User.class,1L);//User的主键id为Long型70em.clear();//将实体管理器中的所有实体变成了游离态(脱管)71user.setName("李寻欢");72em.merge(user);//处于游离态的实体必须使用该方法才能更新73em.getTransaction().commit();74em.close();75}76777879/**80*删除用户81*/82@Test83publicvoiddeleteUser(){84EntityManagerem=emf.createEntityManager();85em.getTransaction().begin();86//查找id为1的User87Useruser=em.find(User.class,1L);//User的主键id为Long型88//进行删除89em.remove(user);90em.getTransaction().commit();91em.close();92}9394/**95*关闭EntityManagerFactory96*/97@After98publicvoidafter(){99if(null!=emf){100emf.close();101}102}103104}
解决方案
解决方案二:
INFO:HHH000318:CouldnotfindanyMETA-INF/persistence.xmlfileintheclasspath找不到配置文件persistence.xml,persistence.xml放至路径是固定的
解决方案三:
2012-11-2115:55:18org.hibernate.annotations.common.Version<clinit>INFO:HCANN000001:HibernateCommonsAnnotations{4.0.1.Final}2012-11-2115:55:18org.hibernate.VersionlogVersionINFO:HHH000412:HibernateCore{4.1.2.Final}2012-11-2115:55:18org.hibernate.cfg.Environment<clinit>INFO:HHH000206:hibernate.propertiesnotfound2012-11-2115:55:18org.hibernate.cfg.EnvironmentbuildBytecodeProviderINFO:HHH000021:Bytecodeprovidername:javassist2012-11-2115:55:18org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImplconfigureINFO:HHH000402:UsingHibernatebuilt-inconnectionpool(notforproductionuse!)2012-11-2115:55:18org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImplconfigureINFO:HHH000115:Hibernateconnectionpoolsize:202012-11-2115:55:18org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImplconfigureINFO:HHH000006:Autocommitmode:true2012-11-2115:55:18org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImplconfigureINFO:HHH000401:usingdriver[com.mysql.jdbc.Driver]atURL[jdbc:mysql://localhost:3306/jpa?useUnicode=true&characterEncoding=UTF-8]2012-11-2115:55:18org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImplconfigureINFO:HHH000046:Connectionproperties:{user=root,password=****,autocommit=true,release_mode=auto}2012-11-2115:55:18org.hibernate.dialect.Dialect<init>INFO:HHH000400:Usingdialect:org.hibernate.dialect.MySQLDialect2012-11-2115:55:18org.hibernate.engine.transaction.internal.TransactionFactoryInitiatorinitiateServiceINFO:HHH000268:Transactionstrategy:org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory2012-11-2115:55:18org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory<init>INFO:HHH000397:UsingASTQueryTranslatorFactory2012-11-2115:55:19org.hibernate.tool.hbm2ddl.SchemaUpdateexecuteINFO:HHH000228:Runninghbm2ddlschemaupdate2012-11-2115:55:19org.hibernate.tool.hbm2ddl.SchemaUpdateexecuteINFO:HHH000102:Fetchingdatabasemetadata2012-11-2115:55:19org.hibernate.tool.hbm2ddl.SchemaUpdateexecuteINFO:HHH000396:Updatingschema2012-11-2115:55:19org.hibernate.tool.hbm2ddl.TableMetadata<init>INFO:HHH000261:Tablefound:jpa.t_user2012-11-2115:55:19org.hibernate.tool.hbm2ddl.TableMetadata<init>INFO:HHH000037:Columns:[id,name,password]2012-11-2115:55:19org.hibernate.tool.hbm2ddl.TableMetadata<init>INFO:HHH000108:Foreignkeys:[]2012-11-2115:55:19org.hibernate.tool.hbm2ddl.TableMetadata<init>INFO:HHH000126:Indexes:[primary]2012-11-2115:55:19org.hibernate.tool.hbm2ddl.SchemaUpdateexecuteINFO:HHH000232:Schemaupdatecomplete2012-11-2115:55:19org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImplstopINFO:HHH000030:Cleaningupconnectionpool[jdbc:mysql://localhost:3306/jpa?useUnicode=true&characterEncoding=UTF-8]我修改了persistence.xml路径,遇到了新的错误,哎,不知道哪的问题
解决方案四:
把错误贴出来
解决方案五:
非常谢谢,我现在调试成功了,原来是我的代码有错误,呵呵!
解决方案六:
代码哪里错了?
解决方案七:
我也是一样的问题,哪里错了