I have a query which works in MySql, but I am trying to use it in Nhibernate, where I am facing problem cause I am a noob in Nhibernate.
SELECT transaction_table.transaction_id,transaction_table.amount,transaction_table.reason,transaction_table.transaction_datetime,g.status,
(CASE
WHEN (transaction_table.user_from IN(0)) THEN 'Paypal'
when (transaction_table.user_from<>0) THEN u.user_fullname
END)as user_name_from ,
(CASE
WHEN (transaction_table.user_to IN(0)) THEN 'Paypal'
when (transaction_table.user_to<>0) THEN us.user_fullname
END)as user_name_to
FROM transaction_table
left outer join users u on u.user_id=transaction_table.user_from
left outer join users us on transaction_table.user_to=us.user_id
left join gateway_table g on g.gateway_table_id=transaction_table.gateway_table_id
This is the query,
var results = session.QueryOver<TransactionTable>(() => transactionAlias)
.Left.JoinAlias(pr => pr.UserFrom, () => usersFromAlias.Id)
.Left.JoinAlias(pr => pr.UserTo, () => usersToAlias.Id)
.Left.JoinAlias(pr => pr.GatewayTableId, () => gatewayAlias.GatewayTableId)
.SelectList(list => list
.Select(pr => pr.TransactionId)
.Select(pr => pr.Amount)
.Select(pr => pr.Reason)
.Select(pr => pr.TransactionDatetime)
.Select(pr => pr.GatewayTableId)
.Select(Projections.Conditional(
Restrictions.Eq(
Projections.Property(() => transactionAlias.UserFrom), 0),
Projections.Constant("Paypal"),
Projections.Property(() => usersFromAlias.FullName)
))
.Select(Projections.Conditional(
Restrictions.Eq(
Projections.Property(() => transactionAlias.UserTo), 0),
Projections.Constant("Paypal"),
Projections.Property(() => usersToAlias.FullName)
)))
.List<object[]>();
I cannot figure out how to proceed after this, this is midway I have been implementing. I cannot find something solid, About XML my mapping might be issue where relations are not mapped, i can't figure that how to do.
Mapping below:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="EWalletModule" namespace="EWalletModule">
<!-- User Table -->
<class name="Users" table="Users">
<id name="Id" column="user_id" type="int">
<generator class="native"></generator>
</id>
<property name="FullName" column="user_fullname" type="String"></property>
<property name="Email" column="user_email" type="String"></property>
<property name="Password" column="user_password" type="String"></property>
<property name="CreatedDatetime" column="created_datetime" type="datetime"></property>
</class>
<!-- Transaction Table -->
<class name="TransactionTable" table="transaction_table">
<id name="TransactionId" column="transaction_id" type="int">
<generator class="native"></generator>
</id>
<property name="GatewayTableId" column="gateway_table_id" type="int"></property>
<property name="UserFrom" column="user_from" type="String"></property>
<property name="UserTo" column="user_to" type="String"></property>
<property name="Amount" column="amount" type="String"></property>
<property name="Reason" column="reason" type="String"></property>
<property name="TransactionDatetime" column="transaction_datetime" type="datetime"></property>
</class>
<!-- Gateway Table -->
<class name="GatewayTable" table="gateway_table">
<id name="GatewayTableId" column="gateway_table_id" type="int">
<generator class="native"></generator>
</id>
<property name="Status" column="status" type="String"></property>
</class>
<!-- Wallet Table -->
<class name="WalletTable" table="user_wallet">
<id name="UserWalletId" column="user_wallet_id" type="int">
<generator class="native"></generator>
</id>
<property name="UserId" column="user_id" type="int"></property>
<property name="WalletBalance" column="wallet_balance" type="float"></property>
</class>
<!-- ManageWallet Table //For Admin -->
<class name="ManageWalletTable" table="manage_wallet">
<id name="ManageWalletId" column="manage_wallet_id" type="int">
<generator class="native"></generator>
</id>
<property name="TransactionFee" column="transaction_fee" type="float"></property>
<property name="WithdrawalFee" column="withdrawal_fee" type="float"></property>
<property name="Tax" column="tax" type="float"></property>
</class>
</hibernate-mapping>
Table is simple like the mapping, but the query works in Mysql workbench and data is alright but no idea about Nhibernate. So currently I get exception issue and I know this is wrong, but cannot figure out how to do it, and I don't want to do it using simple sql query, I will love to do it Object Way.
Thanks to Stack-overflow Community.