我正在创建一个Java应用程序,我正在使用log4j.我已经给出了配置log4j文件的绝对路径以及生成的日志文件的绝对路径(生成此日志文件的位置).我可以在运行时通过以下方式获取Java Web应用程序的绝对路径:
String prefix = getServletContext().getRealPath("/");
Run Code Online (Sandbox Code Playgroud)
但在普通Java应用程序的上下文中,我们可以使用什么?
当你使用spring和Hibernate时,你有没有遇到过日志警告
警告o.hibernate.ejb.HibernatePersistence - HHH015016:遇到了一个弃用的javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; 请改用[org.hibernate.jpa.HibernatePersistenceProvider].
怎么处理?谢谢你的回答.
我正在使用JPA开发JavaSE应用程序.不幸的是,我null打电话后:
Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
您将在下面找到:
EntityManagerFactory并意外返回nullpersistence.xml档案我的代码片段:
public class Main {
private static final String PERSISTENCE_UNIT_NAME = "MeineJpaPU";
private static EntityManagerFactory factory;
public static void main(String[] args) {
// I get null on this line!!!
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
// do stuff with entity manager
...
}
}
Run Code Online (Sandbox Code Playgroud)
我的persistence.xml:
<?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
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="MeineJpaPU" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>path.to.package.server.Todo</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.url" …Run Code Online (Sandbox Code Playgroud) 我很喜欢这个,而且 - 虽然我学到了很多 - 但开始绝望了.
我已经尝试过关于这个优秀问题的所有建议:
我曾经使用无处不在的HibernateUtil类在某一点上工作,但被告知要转到普通的JPA样式:
不幸的是,我无法让bean注入在spring-boot中正常工作.这是我的尝试:
Spring JPA(Hibernate)没有类型的限定bean:javax.persistence.EntityManagerFactory
经过这条路径的大量工作后,我最终得到了一个空实体管理器.我发现了这一点并开始认为它不起作用:
在Tomcat 6中使用JPA2:@PersitenceContext不起作用,EntityManager为null
在我看来,EntityManagerFactory绝对应该是spring-boot创建的任何上下文中的bean,但是......无论如何.我认为至少这会起作用:
申请发布:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
控制器:
@Controller
public class GetController {
private static final String PERSISTENCE_UNIT_NAME = "cpJpaPu";
@RequestMapping(value = "/user", method = RequestMethod.GET)
public @ResponseBody User getUser(@RequestParam(value="id", required=true) int id) {
User user = null;
EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = emf.createEntityManager();
UserDAO userDao = new UserDAO();
userDao.setEntityManager(em); …Run Code Online (Sandbox Code Playgroud) 我知道关于这个问题还有其他问题,但没有一个解决方案对我有用.我正在使用maven在eclipse中构建一个java项目,我在src/main/resource/META_INF文件夹中有我的persistence.xml文件.但是当我尝试安装mvn时,我总是得到这个错误:
No Persistence provider for EntityManager named plasma.persistence
Run Code Online (Sandbox Code Playgroud)
通过控制台输出查看它似乎是由此引起的:
[org.hibernate.jpa.boot.internal.PersistenceXmlParser] - HHH000318: Could not find any META-INF/persistence.xml file in the classpath
Run Code Online (Sandbox Code Playgroud)
这是我的persistence.xml文件:
<persistence 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 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="plasma.persistence" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.spatial.dialect.postgis.PostgisDialect"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432:xxxxx"/>
<property name="hibernate.connection.username" value="xxxxx"/>
<property name="hibernate.connection.password" value="xxxxx"/>
<property name="hibernate.connection.pool_size" value="5"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.max_fetch_depth" value="5"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
Run Code Online (Sandbox Code Playgroud)
我试图右键单击项目并将META_INF文件夹添加到构建路径,但它仍然无效.有任何想法吗?
我正在使用JPA 2.1示例应用程序Hibernate 4.3.x实现.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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_2_1.xsd">
<persistence-unit name="unit1">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>net.roseindia.model.Product</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/common"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)
在pom.xml我有以下依赖.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.5.Final</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
示例命令行应用程序正常工作(非常简单)但是在启动它时会收到以下警告消息.
Apr 13, 2014 1:12:43 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Run Code Online (Sandbox Code Playgroud)
那么,这是我的错误配置问题(我可以避免它吗?),或者它是Hibernate实现中的问题?
更新
这是我使用的代码:
import net.roseindia.model.Product;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory; …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用pgadmin3连接到ubuntu中的Postgresql9.1.我的Pgadmin3 GUI工具没有通过右键单击数据库来提供创建表的任何选项,但在我看到的一些视频中可以使用它.因此,我使用终端创建数据库,它出现在pgadmin3中.
我的Userdetails文件
package org.nitish.hiber;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class UserDetails {
@Id
private int userId;
private String userName;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
Run Code Online (Sandbox Code Playgroud)
我的HibernateCaller文件
package org.nitish.caller;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.nitish.hiber.UserDetails;
public class HibernateTest {
public static void main(String[] args) {
UserDetails user …Run Code Online (Sandbox Code Playgroud) 环境:Windows 7,NetBean 6.9(包括GlassFish v3,Java EE 6),MySQL服务器
我已经在MySQL数据库中创建了表,并通过右键单击项目并选择" create entity from database" 来使用NetBean的功能(对不起,如果措辞错误,因为我的NetBean是日语)
这将创建实体.
现在我去测试我是否可以通过实体管理器访问数据库.
Tmp.java
package local.test.tmp;
import Resources.Users;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;
/**
*
* @author m-t
*/
public class Tmp {
private static EntityManagerFactory factory;
private static final String WEB_PU = "WebApplication1PU";
public static void main(String args[]) {
factory = Persistence.createEntityManagerFactory(WEB_PU);
EntityManager em = factory.createEntityManager();
//read existing entries and write to concole
Query q = em.createQuery("select * from users");
List<Users> userList …Run Code Online (Sandbox Code Playgroud) java ×8
jpa ×5
hibernate ×4
spring ×2
eclipse ×1
filepath ×1
java-ee ×1
jdbc ×1
jpa-2.1 ×1
maven ×1
netbeans-6.9 ×1
postgresql ×1
spring-boot ×1
ubuntu-14.04 ×1