IntellJ 13.x JPA Persistence Sample
跟上一篇差不多,一些基本的东西。
这次是JPA + Spring MVC 3.0
1.建立Project

2.Add JPA Support

3.我们以Hibernate为例,设置JPA的Provider为Hibernate

4.添加persistence.xml,这里标准的位置应该是src/main/resources/META-INF,所以我们要建立resource和META-INF的文件夹

5.回到project structure->modules->JPA,给JPA添加我们刚刚建立的persistence.xml文件

6.修改POM.XML添加两个jar,一个是hibernate-entitymanager,一个是mysql connector

7.修改一下我们的persistence.xml,(这里你也可以先不添加persistence.xml,只是把META-INF建好,然后从第5步那生成也可以)
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="personDB">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="9ijn)OKM"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
7.View->Tools windows->DataBase建立一个DataSource

8.View->Tools Windows->Persistence->Generate Persistence Mapping->By Database Schema

选定位置

9.这时候我们已经有了生成出来的Entity

10. 注意这个时候你如果Console->来做hql查询的时候,他会显示错误,这个现在你可以不用理会,等build之后自己就好了

11.这个时候检查一下persistence.xml文件,主要看一下,class是不是已经加到xml里面,完整的persistence.xml应该是这样
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="personDB">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.springapp.modlels.OfficeEntity</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="9ijn)OKM"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
12.加一个add.jsp,在修改一下controller
package com.springapp.mvc; import com.springapp.modlels.OfficeEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence; @Controller
@RequestMapping("/")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Hello world!");
return "hello";
} @RequestMapping(method = RequestMethod.GET, value="add")
public String add(ModelMap model) {
model.addAttribute("message", "Hello world!");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("personDB");
EntityManager mgr = emf.createEntityManager();
mgr.getTransaction().begin();
OfficeEntity officeEntity = new OfficeEntity();
officeEntity.setOfficeName("test");
mgr.persist(officeEntity);
mgr.getTransaction().commit();
return "add";
} }
运行就可以了。
这时候如果我们返回persistence,console hql的话,就没有问题了

注意,在JBoss下会出现No suitable Driver 的问题,是从getTransaction()开始,不知道为什么会出现这种情况,在Tomcat下运行正常。
IntellJ 13.x JPA Persistence Sample的更多相关文章
- JPA persistence
Play provides a set of very useful helpers to simplify the management of your JPA entities. Note tha ...
- Spring data jpa persistence .xml 配置文件
<?xml version="1.0" encoding="UTF-8"?><persistence xmlns="http://j ...
- 摘抄JPA官方文档原文 防呆
Spring Data JPA - Reference Documentation Oliver GierkeThomas DarimontChristoph StroblMark PaluchVer ...
- jpa注解
http://www.oracle.com/technetwork/cn/middleware/ias/toplink-jpa-annotations-100895-zhs.html#ManyToOn ...
- JPA 批注参考
body, p, th, td, li, ul, ol, h1, h2, h3, h4, h5, h6, pre { font-family: simsun; line-height: 1.4; } ...
- jpa table主键生成策略
用 table 来生成主键详解 它是在不影响性能情况下,通用性最强的 JPA 主键生成器.这种方法生成主键的策略可以适用于任何数据库,不必担心不同数据库不兼容造成的问题. initialValue不起 ...
- JPA 相关API (一)
[Query 接口下的常用API] [API 测试类:Test_QueryAPI.java] 1 package org.zgf.jpa.entity; 2 3 import java.math.Bi ...
- SpringData JPA详解
Spring Data JPA 1. 概述 Spring JPA通过为用户统一创建和销毁EntityManager,进行事务管理,简化JPA的配置等使用户的开发更加简便. Spring Data ...
- hdu3652(含有13且能被13整除的数)数位DP基础
B-number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
随机推荐
- windows下实现微秒级的延时
windowsintegeriostream汇编嵌入式任务 最近正在做一个嵌入式系统,是基于windows ce的,外接硬件的时序要微秒级的延时.1.微秒级的延时肯定不能基于消息(SetTimer函数 ...
- VS2015中DataGridView的DataGridViewComBoboxCell列值无效及数据绑定错误的解决方法
在VS2015中练习DataGridView的使用, 发现其中的DataGridViewComBoboxCell列存在着绑定数据库列后出现值无效的提示 根据网上的解决办法,添加了DataError后可 ...
- windows服务控制类
/// <summary> /// 服务调用控制 /// </summary> public class WinServiceController { /// <summ ...
- 解决ccSvcHst.exe CPU占用超50%的问题,及其缘由
无意中发现任务管理器中一个非常奇特的进程,迅速吃掉了我50%的cpu资源,并且是持续性的,于是上google一番查找,终于有了新的发现. 非常多问答产品所有都是清一色的 错误解决方式: 正常情况下,系 ...
- PI-webservice05-SAP调用外部webservice
在用webservice进行数据传输的过程中,SAP系统与.net开发的信息系统之间的数据调用.如何用SAP调用外部的.net系统发布的webservice程序来获取外部的数据,详情请见下文: 1,创 ...
- 调用Dll里面的窗体
将窗体资源分装到DLL中并且调用 用Delphi生成DLL并封装窗体的示例 调用Dll里面的窗体 DLL文件 library Project2;{ Important note about DLL m ...
- leetcode -- Merge k Sorted Lists add code
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. [ ...
- SQL SERVER中架构的理解
在sqlserver 2005中,可能大家在工作或学习的时候会经常发现这样一些问题,你使用一个账户在数据库中创建了一张表,却发现你自己创建的表却没有修改和查询的权限,这是一件很郁闷的事情,在sqlse ...
- Mysql中自增字段(AUTO_INCREMENT)的一些常识
Mysql中自增字段(AUTO_INCREMENT)的一些常识: http://chengxuyuan.naxieshir.com/fenlei/2/p/151.html
- 如何判断一个数是否为素数(zt)
怎么判断一个数是否为素数? 笨蛋的作法: bool IsPrime(unsigned n){ if (n<2) { //小于2的数即不是合数也不是素数 throw 0; ...