`

hibernate返回非受管实体(Returning non-managed entities)不能别名注射?

阅读更多

 直接上码!

public class OrderDao extends BaseDao<String, Orders> implements IOrderDao {

	@SuppressWarnings("unchecked")
	public List<OrderDTO> getOrdersByUsername(String username) {
		
		String sql = "SELECT"
		      + " BILLADDR1 as {billaddr1},"
		      + "BILLADDR2 as {billaddr2},"
		      + "BILLCITY as {billCity},"
		      + "BILLCOUNTRY as {billCountry},"
		      + "BILLSTATE as {billState},"
		      + "BILLTOFIRSTNAME as {billToFirstName},"
		      + "BILLTOLASTNAME as {billToLastName},"
		      + "BILLZIP as {billZip},"
		      + "SHIPADDR1 as {shipaddr1},"
		      + "SHIPADDR2 as {shipaddr2},"
		      + "SHIPCITY as {shipCity},"
		      + "SHIPCOUNTRY as {shipCountry},"
		      + "SHIPSTATE as {shipState},"
		      + "SHIPTOFIRSTNAME as {shipToFirstName},"
		      + "SHIPTOLASTNAME as {shipToLastName},"
		      + "SHIPZIP as {shipZip},"
		      + "CARDTYPE as {cardType},"
		      + "COURIER as {courier},"
		      + "CREDITCARD as {creditCard},"
		      + "EXPRDATE as {exprdate},"
		      + "LOCALE as {locale},"
		      + "ORDERDATE as {orderDate},"
		      + "ORDERS.ORDERID as {orderId},"
		      + "TOTALPRICE as {totalPrice},"
		      + "USERID as {userid},"
		      + "STATUS as {status}"
		      + " FROM ORDERS ,ORDERSTATUS"
		      + " WHERE ORDERS.USERID = ?"
		      + " AND ORDERS.ORDERID = ORDERSTATUS.ORDERID"
		      + " ORDER BY ORDERDATE";
		Query query = this.getSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(OrderDTO.class));
		query.setString(0, username);
		return query.list();
	}

 上面是ssh集成,hibernate查询。

 

返回非受管实体,结果转换器返回OrderDTO的列表。

 

以下是OrderDTO类:

public class OrderDTO implements Serializable {

	private static final long serialVersionUID = 6321792448424424931L;

	private int orderId;
//	private String username;
	private String userid;
	private Date orderDate;
//	private String shipAddress1;
//	private String shipAddress2;
	private String shipaddr1;
	private String shipaddr2;
	private String shipCity;
	private String shipState;
	private String shipZip;
	private String shipCountry;
	// private String billAddress1;
	// private String billAddress2;
	private String billaddr1;
	private String billaddr2;
	private String billCity;
	private String billState;
	private String billZip;
	private String billCountry;
	private String courier;
	private BigDecimal totalPrice;
	private String billToFirstName;
	private String billToLastName;
	private String shipToFirstName;
	private String shipToLastName;
	private String creditCard;
//	private String expiryDate;
	private String exprdate;
	private String cardType;
	private String locale;
	private String status;
	private List<LineItemDTO> lineItems = new ArrayList<LineItemDTO>();

 get,set方法省略。

 

执行上面的查询,有语法错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax 
to use near 'BILLADDR2 as ,BILLCITY as ,BILLCOUNTRY as ,BILLSTATE as ,BILLTOFIRSTNAME as ,BIL' at line 1

 应该是不能取别名。

 

如果不用别名注射(alias injection names) ,OrderDTO中属性不能使用驼峰命名,属性必须全部小写才能正确返回。

 

求教育!求爽!

0
3
分享到:
评论
2 楼 frankco 2013-01-13  
Mr.TianShu 写道
SELECT  BILLADDR1 as {billaddr1} .......

as {billaddr1},为什么需要加上 "{"  "}" ? 我第一次见。

可以这样用
1 楼 Mr.TianShu 2012-12-24  
SELECT  BILLADDR1 as {billaddr1} .......

as {billaddr1},为什么需要加上 "{"  "}" ? 我第一次见。

相关推荐

    HibernateAPI中文版.chm

    16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 16.2.1. 使用return-property来明确地指定字段/别名 ...

    hibernate3.2中文文档(chm格式)

    16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 16.2.1. 使用return-property来明确地指定字段/别名 ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 16.2.1. 使用return-property来明确地指定字段/别名 ...

    Hibernate+中文文档

    16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 16.2.1. 使用return-property来明确地指定字段/别名 ...

    returning-DOM-elements-javascript-源码.rar

    returning-DOM-elements-javascript-源码.rar

    Python库 | django_pg_returning-1.0.0-py2.py3-none-any.whl

    python库。 资源全名:django_pg_returning-1.0.0-py2.py3-none-any.whl

    Hibernate中文详细学习文档

    16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 16.2.1. 使用return-property来明确地指定字段/别名 ...

    Hibernate 中文 html 帮助文档

    16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 16.2.1. 使用return-property来明确地指定字段/别名 16.2.2. ...

    NHibernate中文帮组文档(2008.11月更新)

    14.1.5. 返回非受管实体(Returning non-managed entities) 14.1.6. 处理继承(Handling inheritance) 14.1.7. 参数(Parameters) 14.2. 命名SQL查询 14.2.1. 使用return-property来明确地指定字段/别名 14.2.2. ...

    Hibernate参考文档

    16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 16.2.1. 使用return-property来明确地指定字段/别名 16.2.2. ...

    NHibernate参考文档 2.0.0 chm

    14.1.5. 返回非受管实体(Returning non-managed entities) 14.1.6. 处理继承(Handling inheritance) 14.1.7. 参数(Parameters) 14.2. 命名SQL查询 14.2.1. 使用return-property来明确地指定字段/别名 14.2.2. ...

    android视频h.264硬编码

    本例可以实现对手机摄像头采集的视频数据硬编码成h.264格式,可以在vlc和EsEye分析工具正常查看。只有视频,没有音频。

    returning-DOM-elements-javascript

    返回-DOM-元素-javascript

    jQuery完全实例.rar

    注意:此处 this 指代的是 DOM 对象而非 jQuery 对象。 HTML 代码: &lt;img/&gt;&lt;img/&gt; jQuery 代码: $("img").each(function(i){ this.src = "test" + i + ".jpg"; }); 结果: [ , &lt;img src="test1.jpg" /&gt; ] ---...

    The_C_Programming_Language-英文版

    4.2 Functions Returning Non-integers 61 4.3 External Variables 63 4.4 Scope Rules 68 4.5 Header Files 69 4.6 Static Variables 70 4.7 Register Variables 71 4.8 Block Structure 71 4.9 Initialization 72 ...

    如何在Spring Boot中使用@AfterReturning注解

    在本文中,我们介绍了Spring Boot AOP中的@AfterReturning注解,它用于定义在目标方法成功返回后执行的切面逻辑。我们通过一个图书管理系统的实例演示了@AfterReturning的使用方式,并在成功查询图书时记录了查询的...

    arduino mysql库函数

    在arduino ide实现单片机读取mysql数据库数据时报错libraries\MySQL_Connector_Arduino\src\MySQL_Encrypt_Sha1.cpp:95:1: error: no return statement in function returning non-void [-Werror=return-type],笔者...

    Rad Studio Delphi C++builder XE 10.4 Patch2

    RSP-28616 [BadCG] Operator Assign should not allow non-default calling conventions RSP-28615 [BadCG] In the absence of Initialize, finalisation is not guaranteed for local variables RSP-28552 Poor ...

    C程序设计语言第2版

    Functions Returning Non-integers External Variables Scope Rules Header Files Static Variables Register Variables Block Structure Initialization Recursion The C Preprocessor File Inclusion ...

Global site tag (gtag.js) - Google Analytics