【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"}

  1. package cn.ycx.study.hibernate.entity;
  2.  
  3. import javax.persistence.Entity;
  4. import javax.persistence.Id;
  5. @Entity
  6. @org.hibernate.annotations.Immutable
  7. @org.hibernate.annotations.Subselect(value="select u.id as id, concat(u.username, '_account') as name from User u")
  8. @org.hibernate.annotations.Synchronize({"User"})
  9. //自定义视图
  10. public class UserView {
  11. @Id
  12. protected long id;
  13. protected String name;
  14. public long getId() {
  15. return id;
  16. }
  17. public void setId(long id) {
  18. this.id = id;
  19. }
  20. public String getName() {
  21. return name;
  22. }
  23. public void setName(String name) {
  24. this.name = name;
  25. }
  26. @Override
  27. public String toString() {
  28. return "UserView [id=" + id + ", name=" + name + "]";
  29. }
  30. }
  1. package cn.ycx.study.hibernate;
  2.  
  3. import static org.junit.Assert.assertTrue;
  4.  
  5. import java.util.Arrays;
  6. import java.util.Collection;
  7. import java.util.List;
  8.  
  9. import org.hibernate.Session;
  10. import org.hibernate.SessionFactory;
  11. import org.junit.After;
  12. import org.junit.Before;
  13. import org.junit.Test;
  14. import org.junit.runner.RunWith;
  15. import org.junit.runners.Parameterized;
  16. import org.junit.runners.Parameterized.Parameters;
  17.  
  18. @RunWith(Parameterized.class)
  19. public class UserViewTest {
  20.  
  21. private Session session = null;
  22.  
  23. public UserViewTest(SessionFactory sessionFactory) {
  24. this.session = sessionFactory.openSession();
  25. }
  26.  
  27. @SuppressWarnings("rawtypes")
  28. @Parameters
  29. public static Collection sessionFactory() {
  30. return Arrays.asList(new Object[]{EnvironmentParameter.getSessionFactoryInstance()});
  31. }
  32.  
  33. @Before
  34. public void setUp() throws Exception {
  35. this.session.beginTransaction();
  36. }
  37.  
  38. @After
  39. public void tearDown() throws Exception {
  40. this.session.getTransaction().commit();
  41. }
  42.  
  43. @Test
  44. public void test() {
  45. List list = this.session.createQuery("select u from UserView u").getResultList();
  46. for(Object o : list) {
  47. System.out.println(o);
  48. }
  49. System.out.println("view execute");
  50. assertTrue(true);
  51. }
  52.  
  53. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <property name="hibernate.connection.username">root</property>
  8. <property name="hibernate.connection.password">123456</property>
  9. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  10. <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
  11. <!-- 设置Hibernate的隔离级别 2:读已提交-->
  12. <property name="hibernate.connection.isolation">2</property>
  13.  
  14. <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
  15. <!-- 控制台 SQL -->
  16. <property name="hibernate.show_sql">true</property>
  17. <!-- 是否对 SQL 格式化 -->
  18. <property name="hibernate.format_sql">true</property>
  19. <!-- 数据库关键字启用引号 -->
  20. <property name="hibernate.auto_quote_keyword">true</property>
  21. <!-- 给表明添加统一前缀
  22. <property name="hibernate.physical_naming_strategy">cn.ycx.study.hibernate.strategy.PhysicalNamingStrategyImpl</property>
  23. -->
  24. <!--
  25. 自动生成数据表的生成策略
  26. create: 每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行。
  27. create-drop: 每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除
  28. update: 第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。
  29. validate: 每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值
  30. -->
  31. <property name="hibernate.hbm2ddl.auto">update</property>
  32.  
  33. <!-- 删除对象后,使其OID置为null
  34. <property name="use_identifier_rollback">true</property>-->
  35.  
  36. <mapping class="cn.ycx.study.hibernate.entity.User"/>
  37. <mapping class="cn.ycx.study.hibernate.entity.Item"/>
  38. <mapping class="cn.ycx.study.hibernate.entity.UserView"/>
  39. </session-factory>
  40. </hibernate-configuration>

【hibernate】应用程序级别的视图的更多相关文章

  1. Asp.net 程序部署问题——在应用程序级别之外使用注册为 allowDefinition='MachineToApplicati错误信息

    [转]在应用程序级别之外使用注册为 allowDefinition='MachineToApplicati 错误信息: ======================================== ...

  2. 错误 1 在应用程序级别之外使用注册为 allowDefinition='

    原文:错误 1 在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的,银流沙 昨天运行一个.NET网站项目时,出现了以下问题: 在应 ...

  3. iOS开发 ~应用程序设计理念:视图控制器(UIViewController)、视图(UIView)

    应用程序设计理念:视图控制器(UIViewController).视图(UIView) 利用视图控制器(底层)管理视图(外观),一对一 1.视图的作用:负责显示的外观 2.视图控制器的作用:创建界面. ...

  4. hibernate ——helloWorld程序(annotation配置)

    在  <hibernate  ——helloWorld程序(XML配置)>基础上,修改.添加部分文件: 1.Teacher类和Teacher表 package com.pt.hiberna ...

  5. 微信小程序之可滚动视图 scroll-view 的使用注意

    微信小程序之可滚动视图 scroll-view 使用注意: 1.scroll-view 中的需要滑动的元素不可以用 float 浮动: 2.scroll-view 中的包裹需要滑动的元素的大盒子用 d ...

  6. 错误 在应用程序级别之外使用注册为 allowDefinition='MachineToApplic

    错误 在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的.如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误. 如果 ...

  7. Hibernate 事物隔离级别 深入探究

    目录 一.数据库事务的定义 二.数据库事务并发可能带来的问题 三.数据库事务隔离级别 四.使用Hibernate设置数据库隔离级别 五.使用悲观锁解决事务并发问题 六.使用乐观锁解决事务并发问题 Hi ...

  8. Hibernate 事物隔离级别

      Hibernate事务和并发控制                                            ++YONG原创,转载请注明 1.    事务介绍: 1.1.        ...

  9. Hibernate注解----类级别注解以及属性注解详解----图片版本

    这篇文章是我在慕课网上学习Hibernate注解的时候进行手机以及整理的笔记. 今天把它分享给大家,希望对大家有用.可以进行收藏,然后需要的时候进行对照一下即可.这样能起到一个查阅的作用. 本文主要讲 ...

随机推荐

  1. webpack3、4的基本的使用方法

    webpack的基本使用 webpack的安装 webpack的使用时需要借助 node 的环境的 在 node 中自动下载了 npm 这个包管理工具,之后的操作我们需要使用npm包管理工具进行相关操 ...

  2. jinjia2

    ansible-playbook --become --become-method=su -K copy.yml - hosts: web remote_user: ansible tasks: - ...

  3. 报错:尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题。

    问题: 在写windows服务时,发布后日志报错:尝试加载 Oracle 客户端库时引发 BadImageFormatException.如果在安装 32 位 Oracle 客户端组件的情况下以 64 ...

  4. 三维目标检测论文阅读:Deep Continuous Fusion for Multi-Sensor 3D Object Detection

    题目:Deep Continuous Fusion for Multi-Sensor 3D Object Detection 来自:Uber: Ming Liang Note: 没有代码,主要看思想吧 ...

  5. [FPGA]Verilog利用PWM调制巧妙完成RGB三色彩虹呼吸灯(给简约的题目以美妙的解答)

    概述 实现彩虹呼吸灯 题目就是这么简短,但这是目前我碰到的最有意思的一道题目,因为他有无数种解决方法,并且每一种都是那么高级或者巧妙,比如 可以利用3路不同初相的PWM调制信号驱动三颗RGB灯重叠呼吸 ...

  6. 2019-11-6:ubuntu 18安装tomcat 9.0

    1,下载tomcat,选择自己想要的版本下载即可 下载官网:http://tomcat.apache.org/ 2,将下载的源码放到自己需要的位置,解压,我新建了一个tomcat目录 sudo mkd ...

  7. VS Code 中文社区正式成立啦!VS Code Day 圆满落幕!

    背景简介 Visual Studio Code 是一款现代化轻量级代码编辑器,它免费.开源.跨平台.功能强大.本次 VS Code Day 是广大 VS Code 爱好者一起学习与交流的盛会,让我们对 ...

  8. c#-EntitySet<TEntity>

    MSDN 解释: https://msdn.microsoft.com/zh-cn/library/bb341748.aspx 为 LINQ to SQL 应用程序中的一对多关系和一对一关系的集合方提 ...

  9. Linux目录结构-中部

    第1章 /proc目录下 1.1 /proc/cpuinfo 系统cpu信息 [root@nfsnobody ~]# cat /proc/cpuinfo                  一般常用的是 ...

  10. shell一键部署nginx+tomcat

    1.首先拉取环境  tomcat需要用到jdk环境 提前准备好nginx源码包,放于指定目录下 vim test.sh #!/bin/bash yum -y install gcc gcc-c++ z ...