zoukankan      html  css  js  c++  java
  • Hibernate createQuery调用joincolumn

    1. Beans

    a. Version Bean

    package locationService.beans;
    
    import java.sql.Timestamp;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.persistence.*;
    
    import org.hibernate.annotations.GenericGenerator;
    
    import locationService.beans.Entities;
    import locationService.beans.Attribute;
    import locationService.beans.AttributeTag;
    import locationService.beans.EntityToEntity;
    import locationService.beans.Tag;
    
    @Entity
    @Table(name = "version")
    public class Version {
      private int versionId;
      private boolean complete;
      private Timestamp editDate;
      private String author;
      private String description;
      private int preVersionId;
      private List<Entities> entities = new ArrayList<Entities>();
      private List<Attribute> attribute = new ArrayList<Attribute>();
      private List<AttributeTag> attributeTag = new ArrayList<AttributeTag>();
      private List<EntityToEntity> entityToEntity = new ArrayList<EntityToEntity>();
      private List<Tag> tag = new ArrayList<Tag>();
      
    
      @Id
      @GeneratedValue(generator = "increment")
      @GenericGenerator(name = "increment", strategy = "increment")
      @Column(name="version_id")
      public int getVersionId() {
    	return versionId;
      }
      public void setVersionId(int versionId) {
    	this.versionId = versionId;
      }
      
      @Column(name="complete")
      public boolean getComplete() {
    	return complete;
      }
      public void setComplete(boolean complete) {
    	this.complete = complete;
      }
    
      @Column(name="edit_date")
      public Timestamp getEditDate() {
    	return editDate;
      }
      public void setEditDate(Timestamp editDate) {
    	this.editDate = editDate;
      }
    
      @Column(name="author")
      public String getAuthor() {
    	return author;
      }
      public void setAuthor(String author) {
    	this.author = author;
      }
    
      @Column(name="description")
      public String getDescription() {
    	return description;
      }
      public void setDescription(String description) {
    	this.description = description;
      }
    
      @Column(name="pre_version_id")
      public int getPreVersionId() {
    	return preVersionId;
      }
      public void setPreVersionId(int preVersionId) {
    	this.preVersionId = preVersionId;
      }
      
    
      @OneToMany(cascade = CascadeType.ALL)
      @JoinColumn(name = "version_id", nullable = false)
      public List<Entities> getEntities() {
    	return entities;
      }
      public void setEntities(List<Entities> entities) {
    	this.entities = entities;
      }
      
      @Override
      public String toString() {
        return "versionId is " + versionId + ", preVersionId is " + preVersionId + ", author is " + author;
      }
    
      
      
    }
    

    b. Entities Bean

    package locationService.beans;
    
    import javax.persistence.*;
    
    import org.hibernate.annotations.GenericGenerator;
    
    @Entity
    @Table(name = "entities")
    public class Entities {
      private int entityId;
      private String label;
      public enum entityType { 
    	 location, group; 
      } 
      
      private entityType locationOrGroup;
      @Id
      @Column(name="entity_id")
      public int getEntityId() {
    	return entityId;
      }
      public void setEntityId(int entityId) {
    	this.entityId = entityId;
      }
    
      @Column(name="label")
      public String getLabel() {
    	return label;
      }
      public void setLabel(String label) {
    	this.label = label;
      }
    
      @Column(name="location_or_group")
      @Enumerated(EnumType.STRING) 
      public entityType getLocationOrGroup() {
    	return locationOrGroup;
      }
      public void setLocationOrGroup(entityType locationOrGroup) {
    	this.locationOrGroup = locationOrGroup;
      }
    
      @Override
      public String toString() {
        return "entityId is " + entityId + ", label is " + label + ", locationOrGroup is " + locationOrGroup;
      }
    
    }
    

    2. Call Method

      /**
       * Check whether this entities id has been in the version
       */
      public boolean checkEntitiesPK (Version version, int entityId) {
        Session session = Config.getSessionFactory().openSession();
        session.beginTransaction();
    	
        int versionId = version.getVersionId();
        
        Query query = session.createQuery("from Entities where entityId = :entityId and version_id = :versionId");
        query.setParameter("entityId", entityId);
        query.setParameter("versionId", versionId);
        
        @SuppressWarnings("unchecked")
        List<Entities> o = query.list();
        if (o == null || o.size() == 0){
        	return false;
        }
    
    	return true;
      }
    

      

  • 相关阅读:
    Vuex ~ 初识
    Vue 2.0 生命周期-钩子函数理解
    vue利用watch侦听对象具体的属性 ~ 巧用计算属性computed做中间层
    Elements in iteration expect to have 'v-bind:key' directives.' 提示错误如何解决?
    微信小程序-如何自定义导航栏(navigationStyle)?
    微信小程序~触摸相关事件(拖拽操作、手势识别、多点触控)
    [Java] Collections
    [Java] Map / HashMap
    [Data Structure] 红黑树( Red-Black Tree )
    [Data Structure] 二叉搜索树(Binary Search Tree)
  • 原文地址:https://www.cnblogs.com/codingforum/p/4328625.html
Copyright © 2011-2022 走看看