【hibernate】应用程序级别的视图
【hibernate】应用程序级别的视图
转载:https://www.cnblogs.com/yangchongxing/p/10361281.html
在没有数据库修改权限时,像创建视图可以使用 hibernate 的应用程序视图。
@org.hibernate.annotations.Immutable 不可变
@org.hibernate.annotations.Subselect(value="select u.id as id, concat(u.username, '_account') as name from User u") 子查询
@org.hibernate.annotations.Synchronize({"User"}) 列出 select 中引用的所有表名,多个表用逗号 {"User", "Role"}
package cn.ycx.study.hibernate.entity; import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
@org.hibernate.annotations.Immutable
@org.hibernate.annotations.Subselect(value="select u.id as id, concat(u.username, '_account') as name from User u")
@org.hibernate.annotations.Synchronize({"User"})
//自定义视图
public class UserView {
@Id
protected long id;
protected String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "UserView [id=" + id + ", name=" + name + "]";
}
}
package cn.ycx.study.hibernate; import static org.junit.Assert.assertTrue; import java.util.Arrays;
import java.util.Collection;
import java.util.List; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class)
public class UserViewTest { private Session session = null; public UserViewTest(SessionFactory sessionFactory) {
this.session = sessionFactory.openSession();
} @SuppressWarnings("rawtypes")
@Parameters
public static Collection sessionFactory() {
return Arrays.asList(new Object[]{EnvironmentParameter.getSessionFactoryInstance()});
} @Before
public void setUp() throws Exception {
this.session.beginTransaction();
} @After
public void tearDown() throws Exception {
this.session.getTransaction().commit();
} @Test
public void test() {
List list = this.session.createQuery("select u from UserView u").getResultList();
for(Object o : list) {
System.out.println(o);
}
System.out.println("view execute");
assertTrue(true);
} }
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
<!-- 设置Hibernate的隔离级别 2:读已提交-->
<property name="hibernate.connection.isolation">2</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 控制台 SQL -->
<property name="hibernate.show_sql">true</property>
<!-- 是否对 SQL 格式化 -->
<property name="hibernate.format_sql">true</property>
<!-- 数据库关键字启用引号 -->
<property name="hibernate.auto_quote_keyword">true</property>
<!-- 给表明添加统一前缀
<property name="hibernate.physical_naming_strategy">cn.ycx.study.hibernate.strategy.PhysicalNamingStrategyImpl</property>
-->
<!--
自动生成数据表的生成策略
create: 每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行。
create-drop: 每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除
update: 第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。
validate: 每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值
-->
<property name="hibernate.hbm2ddl.auto">update</property> <!-- 删除对象后,使其OID置为null
<property name="use_identifier_rollback">true</property>--> <mapping class="cn.ycx.study.hibernate.entity.User"/>
<mapping class="cn.ycx.study.hibernate.entity.Item"/>
<mapping class="cn.ycx.study.hibernate.entity.UserView"/>
</session-factory>
</hibernate-configuration>
【hibernate】应用程序级别的视图的更多相关文章
- Asp.net 程序部署问题——在应用程序级别之外使用注册为 allowDefinition='MachineToApplicati错误信息
[转]在应用程序级别之外使用注册为 allowDefinition='MachineToApplicati 错误信息: ======================================== ...
- 错误 1 在应用程序级别之外使用注册为 allowDefinition='
原文:错误 1 在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的,银流沙 昨天运行一个.NET网站项目时,出现了以下问题: 在应 ...
- iOS开发 ~应用程序设计理念:视图控制器(UIViewController)、视图(UIView)
应用程序设计理念:视图控制器(UIViewController).视图(UIView) 利用视图控制器(底层)管理视图(外观),一对一 1.视图的作用:负责显示的外观 2.视图控制器的作用:创建界面. ...
- hibernate ——helloWorld程序(annotation配置)
在 <hibernate ——helloWorld程序(XML配置)>基础上,修改.添加部分文件: 1.Teacher类和Teacher表 package com.pt.hiberna ...
- 微信小程序之可滚动视图 scroll-view 的使用注意
微信小程序之可滚动视图 scroll-view 使用注意: 1.scroll-view 中的需要滑动的元素不可以用 float 浮动: 2.scroll-view 中的包裹需要滑动的元素的大盒子用 d ...
- 错误 在应用程序级别之外使用注册为 allowDefinition='MachineToApplic
错误 在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的.如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误. 如果 ...
- Hibernate 事物隔离级别 深入探究
目录 一.数据库事务的定义 二.数据库事务并发可能带来的问题 三.数据库事务隔离级别 四.使用Hibernate设置数据库隔离级别 五.使用悲观锁解决事务并发问题 六.使用乐观锁解决事务并发问题 Hi ...
- Hibernate 事物隔离级别
Hibernate事务和并发控制 ++YONG原创,转载请注明 1. 事务介绍: 1.1. ...
- Hibernate注解----类级别注解以及属性注解详解----图片版本
这篇文章是我在慕课网上学习Hibernate注解的时候进行手机以及整理的笔记. 今天把它分享给大家,希望对大家有用.可以进行收藏,然后需要的时候进行对照一下即可.这样能起到一个查阅的作用. 本文主要讲 ...
随机推荐
- 【前端vue进阶实战】:从零打造一个流程图、拓扑图项目【Nuxt.js + Element + Vuex】 (一)
本系列教程是用Vue.js + Nuxt.js + Element + Vuex + 开源js绘图库,打造一个属于自己的在线绘图软件,最终效果:topology.le5le.com .如果你觉得好,欢 ...
- 力扣(LeetCode)三个数的最大乘积 个人题解
给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积. 示例 1: 输入: [1,2,3] 输出: 6 示例 2: 输入: [1,2,3,4] 输出: 24 注意: 给定的整型数组长度 ...
- 构思一个在windows下仿objc基于动画层ui编程的ui引擎
用c/c++编程有些年了,十个指头可以数齐,在涉入iOS objc开发后,有种无比舒服的感觉,尤其在UI开发上. 在QuartzCore.framework下动画和透明窗口等许多效果的事都变得那么方便 ...
- rhel-6.3-i386安装samba
1.安装samba 1).安装软件 rpm –ivh /mnt/Packages/samba-3.5.10-125.el6.i686.rpm 2).创建用户 useradd myadmin 3).设置 ...
- vux组件的全局注册引入
安装好vux后,要引入全局组件是要在main.js中使用Vue.component引入的,不能直接使用Vue.use,不能直接使用Vue.use,不能直接使用Vue.use import router ...
- tomcat-9.0.20部署后输出窗口乱码解决方案
问题:启动tomcat的时候,窗口乱码,默认都是UTF-8的,但是控制台是GBK的,要保持一致 可以通过控制台查看本机的编码: : 936 代表 GB2312 解决办法:打开tomcat目录下的c ...
- SpringSecurity动态加载用户角色权限实现登录及鉴权
很多人觉得Spring Security实现登录验证很难,我最开始学习的时候也这样觉得.因为我好久都没看懂我该怎么样将自己写的用于接收用户名密码的Controller与Spring Security结 ...
- HotSpot虚拟机对象的创建过程
1.文中讨论的对象限于普通Java对象,不包括数组和class对象. 2.内存的分配方式由Java堆是否规整来决定,而Java堆是否规整取决于垃圾收集器是否有压缩整理的功能. 3.还需要考虑:对象的创 ...
- PostGIS 存储过程返回类型
Postgresql存储过程返回值的方式有很多,在此先只记录一下自己用到过的,慢慢拓展 1.type型,这里geometry可以是任何postgresql支持的类型(integer/text/char ...
- 【python测试开发栈】python内存管理机制(一)—引用计数
什么是内存 在开始进入正题之前,我们先来回忆下,计算机基础原理的知识,为什么需要内存.我们都知道计算机的CPU相当于人类的大脑,其运算速度非常的快,而我们平时写的数据,比如:文档.代码等都是存储在磁盘 ...