• 【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>
  • 相关阅读:
    什么是 MyBatis?
    @RequestMapping 注解用在类上面有什么作用?
    如果你也用过 struts2.简单介绍下 springMVC 和 struts2 的区别有哪些?
    SpringMVC 流程?
    SpringMVC 工作原理?
    什么是 SpringMvc?
    依赖注入的三种实现方式?
    什么是IOC
    spring的作用
    什么是spring框架?
  • 原文地址:https://www.cnblogs.com/yangchongxing/p/10361281.html
Copyright © 2020-2023  润新知