先放两个个类
PoetriesEntity.java:
@Entity
@Table(name = "poetries", schema = "tang_poetry")
public class PoetriesEntity {
private int id;
private int poetId;
private String content;
private String title;
private Timestamp createdAt;
private Timestamp updatedAt;
private PoetsEntity poetsEntity;
@JoinColumn(name = "poet_id",unique = true)
@ManyToOne(cascade = { CascadeType.ALL })
public PoetsEntity getPoetsEntity() {
return poetsEntity;
}
public void setPoetsEntity(PoetsEntity poetsEntity) {
this.poetsEntity = poetsEntity;
}
@Id
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column
public int getPoetId() {
return poetId;
}
public void setPoetId(int poetId) {
this.poetId = poetId;
}
@Basic
@Column(name = "content")
@Type(type="text")
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Basic
@Column(name = "title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Basic
@Column(name = "created_at")
public Timestamp getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Timestamp createdAt) {
this.createdAt = createdAt;
}
@Basic
@Column(name = "updated_at")
public Timestamp getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Timestamp updatedAt) {
this.updatedAt = updatedAt;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PoetriesEntity that = (PoetriesEntity) o;
if (id != that.id) return false;
if (poetId != that.poetId) return false;
if (content != null ? !content.equals(that.content) : that.content != null) return false;
if (title != null ? !title.equals(that.title) : that.title != null) return false;
if (createdAt != null ? !createdAt.equals(that.createdAt) : that.createdAt != null) return false;
if (updatedAt != null ? !updatedAt.equals(that.updatedAt) : that.updatedAt != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + poetId;
result = 31 * result + (content != null ? content.hashCode() : 0);
result = 31 * result + (title != null ? title.hashCode() : 0);
result = 31 * result + (createdAt != null ? createdAt.hashCode() : 0);
result = 31 * result + (updatedAt != null ? updatedAt.hashCode() : 0);
return result;
}
}
PoetsEntity.java:
@Entity
@Table(name = "poets", schema = "tang_poetry")
public class PoetsEntity implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
private Timestamp createdAt;
private Timestamp updatedAt;
private Long countQuery;
private String nameQuery;
@Id
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "created_at")
public Timestamp getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Timestamp createdAt) {
this.createdAt = createdAt;
}
@Basic
@Column(name = "updated_at")
public Timestamp getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Timestamp updatedAt) {
this.updatedAt = updatedAt;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PoetsEntity that = (PoetsEntity) o;
if (id != that.id) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
if (createdAt != null ? !createdAt.equals(that.createdAt) : that.createdAt != null) return false;
if (updatedAt != null ? !updatedAt.equals(that.updatedAt) : that.updatedAt != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (createdAt != null ? createdAt.hashCode() : 0);
result = 31 * result + (updatedAt != null ? updatedAt.hashCode() : 0);
return result;
}
public Long getCountQuery() {
return countQuery;
}
public void setCountQuery(Long countQuery) {
this.countQuery = countQuery;
}
public String getNameQuery() {
return nameQuery;
}
public void setNameQuery(String nameQuery) {
this.nameQuery = nameQuery;
}
public PoetsEntity(Long countQuery, String nameQuery){
this.countQuery = countQuery;
this.nameQuery = nameQuery;
}
public PoetsEntity(){
}
}
Main.java:
public class Main {
private static final SessionFactory ourSessionFactory;
static {
try {
Configuration configuration = new Configuration();
configuration.configure();
ourSessionFactory = configuration.buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession() throws HibernateException {
return ourSessionFactory.openSession();
}
public static void closeSession() throws HibernateException {
if (ourSessionFactory != null) {
ourSessionFactory.close();
}
}
public static void main(final String[] args) throws Exception {
HQLPractise();
System.out.println("\n=====================\n");
pagePractise();
System.out.println("\n=====================\n");
mapPractise();
closeSession();
}
public static void HQLPractise() {
Transaction tx = null;
Session session = null;
try {
session = getSession();
tx = session.beginTransaction();
// String hql = "select new PoetsEntity(count(ps.id),p.name) from PoetriesEntity ps join PoetsEntity p on ps.poetId = p.id where length(p.name)=9 group by p.name";
String hql = "select new PoetsEntity(count(ps.id),ps.poetsEntity.name) from PoetriesEntity ps where length(ps.poetsEntity.name)=9 group by ps.poetsEntity.name";
Query query = session.createQuery(hql);
for (Object object : query.list()) {
System.out.println("姓名:" + ((PoetsEntity) object).getNameQuery() + " 诗词数量:" + ((PoetsEntity) object).getCountQuery() + "\t");
}
} catch (Exception e) {
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
} finally {
if (session != null) {
session.close();
}
}
}
public static void pagePractise() {
Transaction tx = null;
Session session = null;
try {
session = getSession();
tx = session.beginTransaction();
// String hql = "select ps.title from PoetriesEntity ps join PoetsEntity p on ps.poetId = p.id where p.name = '李白'";
String hql = "select ps.title from PoetriesEntity ps where ps.poetsEntity.name = :name";
Query query = session.createQuery(hql).setParameter("name", "李白");
int count = (query.list().size() / 10);
if (query.list().size() % 10 != 0) {
count += 1;
}
System.out.println("诗人李白相关的数据总共 " + count + " 页。");
for (int i = 0; i < count; i++) {
System.out.println("第" + (i + 1) + "页内容");
if (i == 0) {
publicMethod(query, 0, 10);
} else {
publicMethod(query, (i * 10), 10);
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
} finally {
if (session != null) {
session.close();
}
}
}
public static void threePractiseHQL() {
System.out.println("请输入要查询的诗人名称:");
Scanner scanner = new Scanner(System.in);
String in = scanner.nextLine();
Transaction tx = null;
Session session = null;
try {
session = getSession();
tx = session.beginTransaction();
// String hql = "select ps.content from PoetriesEntity ps join PoetsEntity p on ps.poetId = p.id where p.name = \'"+in + "\'";
String hql = "select ps.content from PoetriesEntity ps where ps.poetsEntity.name = :name";
Query query = session.createQuery(hql).setParameter("name", in);
List list = query.list();
for (Object object : list) {
System.out.println(object.toString().substring(0, 16));
}
} catch (Exception e) {
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
} finally {
if (session != null) {
session.close();
}
}
}
public static void mapPractise() {
System.out.println("请输入要查询的诗人名称:");
Scanner scanner = new Scanner(System.in);
String in = scanner.nextLine();
Transaction tx = null;
Session session = null;
try {
session = getSession();
tx = session.beginTransaction();
List list = session.createQuery("select ps.content from PoetriesEntity ps where ps.poetsEntity.name = :name order by ps.id DESC").setParameter("name", in).list();
for (Object object : list) {
System.out.println(object.toString().substring(0, 15));
}
} catch (Exception e) {
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
} finally {
if (session != null) {
session.close();
}
}
}
public static void publicMethod(Query query, int i, int j) {
List listTitle = query.setFirstResult(i).setMaxResults(j).list();
for (Object object : listTitle) {
System.out.println("《" + object.toString() + "》");
}
}
}
在PoetriesEntity中添加了外键说明,之后hql语句就可以直接使用ps.poetsentity.name,不需要再使用join ... on ... 这样注释中的sql语句。。