临时表关联查询
User.java
package domain;
import javax.persistence.Column;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Formula;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private Long id;
@Column(name="username")
private String usrename;
@Column(name="address_id")
private Long addressId;
@Formula(value="(select count(1) from address)")
private Integer addressCount;
public Integer getAddressCount() {
return addressCount;
}
public void setAddressCount(Integer addressCount) {
this.addressCount = addressCount;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsrename() {
return usrename;
}
public void setUsrename(String usrename) {
this.usrename = usrename;
}
public Long getAddressId() {
return addressId;
}
public void setAddressId(Long addressId) {
this.addressId = addressId;
}
}
Address.java
package domain;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.annotations.Formula;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import javax.persistence.Column;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
@Table(name="address")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private Long id;
@Column(name="address_name")
private String addressName;
//@Formula("(select count(*) from user u where u.address_id = id) ")
@Transient
private Integer matchCount;
@OneToOne(targetEntity=Temp.class)
@JoinColumn(name = "id",referencedColumnName = "address_id",foreignKey =@ForeignKey(value=ConstraintMode.NO_CONSTRAINT),insertable=false,updatable=false)
@NotFound(action=NotFoundAction.IGNORE)
private Temp temp;
public Temp getTemp() {
return temp;
}
public void setTemp(Temp temp) {
this.temp = temp;
}
public Integer getMatchCount() {
return matchCount;
}
public void setMatchCount(Integer matchCount) {
this.matchCount = matchCount;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getAddressName() {
return addressName;
}
public void setAddressName(String addressName) {
this.addressName = addressName;
}
}
Temp.java
package domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.annotations.Subselect;
@Entity(name="temp")
@Subselect(value="select id as userid,count(1) as usercount from user group by id")
public class Temp {
@Id
private Long userid;
private Integer usercount;
public Long getUserid() {
return userid;
}
public void setUserid(Long userid) {
this.userid = userid;
}
public Integer getUsercount() {
return usercount;
}
public void setUsercount(Integer usercount) {
this.usercount = usercount;
}
}
Main.java
package domain;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.annotations.FilterJoinTable;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.JoinType;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate5.HibernateTemplate;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
SessionFactory sf = (SessionFactory) ctx.getBean("sessionFactory");
HibernateTemplate t = (HibernateTemplate) ctx.getBean("hibernateTemplate");
DetachedCriteria criteria = DetachedCriteria.forClass(Address.class);
criteria.createCriteria("temp", JoinType.LEFT_OUTER_JOIN);
List<Address> a = (List<Address>) t.findByCriteria(criteria);
System.out.println(a.get(0).getTemp().getUsercount());
}
}

浙公网安备 33010602011771号