67,515
社区成员
发帖
与我相关
我的任务
分享
package com.test.hibernate;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Code {
private int id;
private String province;
private String city;
private String county;
private int postcode;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCounty() {
return county;
}
public void setCounty(String county) {
this.county = county;
}
public int getPostcode() {
return postcode;
}
public void setPostcode(int postcode) {
this.postcode = postcode;
}
}
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/code</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<!--
<mapping resource="com/test/hibernate/Code.hbm.xml"/>
-->
<mapping class="com.test.hibernate.Code"/>
</session-factory>
</hibernate-configuration>
package com.test.hibernate;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class Test {
public static void main(String[] args) {
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
SessionFactory sessionFactory = configuration
.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
//String hql = "select c.province,c.city from Code c where c.province='湖南省'";
String hql2 = "select province,city from code where province = '湖南省' group by city";
SQLQuery query = session.createSQLQuery(hql);
List<Object[]> result = (List<Object[]>)query.list();
System.out.println("---result------>"+result);
session.beginTransaction();
session.getTransaction().commit();
session.close();
}
}
String province = "北京市";
String hql = "select new List(c.province as province,c.city as city) from Code c where c.province =:province ";
Query q = (Query) session.createQuery(hql);
//q.setString("province", province);
q.setEntity(0, province);
List<Object[]> result = q.list();
System.out.println("---result------>"+result);
还是我的where语句写错了呢?