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中属性不能使用驼峰命名,属性必须全部小写才能正确返回。

 

求教育!求爽!

你可能感兴趣的:(Hibernate,createSQLQuery)