Getting started with NHibernate
How can I generate identity fields in nHibernate using Hilo algorithm?
use class="hilo":
<generator class="hilo">
example:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernate__MyClass" assembly="NHibernate__MyClass">
<class name="MyClass" table="MyClass">
<id name="Id" type="int" column="ID">
<generator class="hilo">
</id>
<property name="Name">
<column name="Name" not-null="true" />
</property>
<property name="Value">
<column name="Value" not-null="true" />
</property>
</class>
</hibernate-mapping>
I simplified:
<id name="Id">
<column name="ID" sql-type="int" not-null="true"/>
<generator class="hilo" />
</id>
to:
<id name="Id" type="int" column="ID">
<generator class="hilo">
</id>
You could have a syntax error of some sort that is confusing NHibernate.
If you could provide more detail about the code that is executing before the failure or anything else you might think is important, that could speed the rate at which your problem is resolved.
I haven't watched the screencasts yet. But Summer of nHibernate should help you.
I am sorry - I am not answering your original question.
Related
I am working on wso2 esb since last 6 months. I want to download attachment from gmail in wso2 esb/ei and save them into local folder.Google it and get solution only for sending attachment using gmail connector, not downloading attachment.
Note: Already seen,but can't able to get solution by referring the following link-
WSO2 esb get attach files from email
Code:
ProxyService
<proxy name="GmailConfigAttachment-TaskProxy" startOnLoad="true"
transports="http https" xmlns="http://ws.apache.org/ns/synapse">
<target>
<inSequence>
<log level="custom">
<property name="Log Text"
value="Inside GmailConfigAttachment-TaskProxy Service"></property>
</log>
<property name="FORCE_SC_ACCEPTED" scope="axis2" type="STRING"
value="true" />
<property description="serviceName" name="serviceName"
scope="operation" type="STRING"
value="TaskScheduler_ASG_Read_Email_Body_Dummy_Service" />
<property description="messageId" expression="get-property('MessageID')"
name="messageId" scope="operation" type="STRING" />
<sequence key="Load_Gmail_Configuration" />
<gmail.init>
<userId>{$ctx:userId}</userId>
<accessToken>{$ctx:accessToken}</accessToken>
<apiUrl>{$ctx:apiUrl}</apiUrl>
<clientId>{$ctx:clientId}</clientId>
<clientSecret>{$ctx:clientSecret}</clientSecret>
<refreshToken>{$ctx:refreshToken}</refreshToken>
<accessTokenRegistryPath>{$ctx:registryPath}</accessTokenRegistryPath>
</gmail.init>
<gmail.listAllMails>
<labelIds>INBOX</labelIds>
<q>is:unread</q>
</gmail.listAllMails>
<log level="full" />
<iterate description="MailIterator" expression="//jsonObject/messages" sequential="true" id="listUnread">
<target>
<sequence>
<log description="Iterate Logger" level="custom" separator=",**, ">
<property expression="fn:concat('GmailConfigAttachment-TaskProxy_Service ESB-MessageId:',get-property('operation','messageId'))" name="LogText" />
<property expression="//id/text()" name="Gmail-MessageId" />
</log>
<property description="emailMsgId" expression="//id/text()" name="emailMsgId" scope="default" type="STRING" />
<property description="emailSubject" expression="//headers/name[text()='Subject']/../value/text()" name="emailSubject" scope="default" type="STRING"/>
<property description="emailDate" expression="//headers/name[text()='Date']/../value/text()" name="emailDate" scope="operation" type="STRING"/>
<property description="toAddress" expression="//headers/name[text()='To']/../value/text()" name="toAddress" scope="operation" type="STRING"/>
<header expression="fn:concat('Bearer ', $ctx:uri.var.gmail.accessToken)" name="Authorization" scope="transport" />
<gmail.readMail>
<id>{$ctx:emailMsgId}</id>
</gmail.readMail>
<payloadFactory media-type="xml">
<format>
<htmlProcessEmailBody xmlns="">
<emailMessageId>$1</emailMessageId>
<emailSubject>$2</emailSubject>
<emailBody>$3</emailBody>
</htmlProcessEmailBody>
</format>
<args>
<arg evaluator="xml" expression="get-property('emailMsgId')"/>
<arg evaluator="xml" expression="get-property('emailSubject')"/>
<arg evaluator="xml" expression="//payload"/>
</args>
</payloadFactory>
<log level="custom">
<property description="emailMsgId" expression="get-property('emailMsgId')" name="emailMsgId" scope="default" type="STRING" />
<property description="emailSubject" expression="//headers/name[text()='Subject']/../value/text()" name="emailSubject" scope="default" type="STRING"/>
<property description="emailDate" expression="//headers/name[text()='Date']/../value/text()" name="emailDate" scope="default" type="STRING"/>
<property description="toAddress" expression="//headers/name[text()='To']/../value/text()" name="toAddress" scope="default" type="STRING"/>
</log>
<!-- here i need further process to download attachment from gmail -->
</sequence>
</target>
</iterate>
</inSequence>
<outSequence />
<faultSequence />
</target>
Output of ESB
So Can anyone help me to provide solution?
Awaiting for your response.
Thank you.
Finally i found the solution for my question.
Idea :
Iterating each parts then getting email attachment id which will be passed to gmail api in order to get base64 encoded format.
Code snippet:
<iterate continueParent="true" description="MailIterator" expression="//parts" id="listUnread" sequential="true">
<target>
<sequence>
<property expression="//filename[text()!=' ']" name="AttachedFileName" scope="default" type="STRING"/>
<property expression="substring-after(get-property('AttachedFileName'),'.')" name="Attachmentextension" scope="default" type="STRING"/>
<filter description="check emailSubject" regex="jpg|jpeg|png|gif|webp|tiff|tif|psd|raw|bmp|dib|heif|heic|indd|ind|jp2" source="lower-case(get-property('Attachmentextension'))">
<then>
<property description="emailAttachmentId" expression="//attachmentId/text()" name="uri.var.attachmentId" scope="default" type="STRING"/>
<log level="custom">
<property expression="get-property('AttachedFileName')" name="=====ValidAttachmentFileName===="/>
<property expression="get-property('uri.var.attachmentId')" name="====emailAttachmentId====="/>
</log>
<header expression="fn:concat('Bearer ', $ctx:uri.var.gmail.accessToken)" name="Authorization" scope="transport"/>
<call>
<endpoint>
<http method="get" uri-template="{+uri.var.gmail.apiUrl}/{+uri.var.gmail.apiVersion}/users/{+uri.var.gmail.userId}/messages/{+uri.var.id}/attachments/{+uri.var.attachmentId}"/>
</endpoint>
</call>
<property description="emailAttachment" expression="//data/text()" name="emailAttachment" scope="default" type="STRING"/>
<!-- ==========Script for Base64 url to Base64 Encoding Format ========== -->
<script language="js"><![CDATA[var log=mc.getServiceLog();
var emailAttachment = mc.getProperty('emailAttachment');
emailAttachment=emailAttachment.replaceAll("_","/").replaceAll("-","+");
mc.setProperty("modifiedemailAttachment",emailAttachment)]]></script>
<log level="custom">
<property expression="get-property('modifiedemailAttachment')" name="========modifiedemailAttachment========"/>
</log>
I am writing a web application with Breeze, OData, WebAPI, and Angular (BOWA) using the integrated WebAPI ASP Identity schema. When breeze consumes the $metadata it is unable to finish and gives a 'null' error and fails. I disabled the ASP Identity to test and it works fine. Is Breeze unable to address more than a single namespace? The namespace is posted below. Are there any known work arounds to this? (OData v3)
<Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm" Namespace="Microsoft.AspNet.Identity.EntityFramework">
<ComplexType Name="IdentityUserRole">
<Property Name="UserId" Type="Edm.String"/>
<Property Name="RoleId" Type="Edm.String"/>
</ComplexType>
<EntityType Name="IdentityUserClaim">
<Key>
<PropertyRef Name="Id"/>
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false"/>
<Property Name="UserId" Type="Edm.String"/>
<Property Name="ClaimType" Type="Edm.String"/>
<Property Name="ClaimValue" Type="Edm.String"/>
</EntityType>
<ComplexType Name="IdentityUserLogin">
<Property Name="LoginProvider" Type="Edm.String"/>
<Property Name="ProviderKey" Type="Edm.String"/>
<Property Name="UserId" Type="Edm.String"/>
</ComplexType>
</Schema>
Take a look at the section "Correcting the namespace for EdmBuilder" on this page: http://www.getbreezenow.com/documentation/odata-server
I have 2 classes like this :
Message(id, title, content)
MessageEmployee(id, messageId, employeeId, readFlag)
and 2 tables like this :
MESSAGE(mess_id, mess_title, mess_content)
MESSAGE_EMPLOYEE(mess_empl_id, mess_id, empl_id, read_fg)
Mapping files :
<hibernate-mapping package="core">
<class name="Message" table="MESSAGE">
<!-- class id -->
<id name="id" type="int" column="MESS_ID" length="11">
<generator class="native"/>
</id>
<property name="content" type="string" column="MESS_CONTENT" />
<property name="title" type="string" column="MESS_TITLE" />
</class>
</hibernate-mapping>
<hibernate-mapping package="core">
<class name="MessageEmployee" table="MESSAGE_EMPLOYEE">
<!-- class id -->
<id name="id" type="int" column="MESS_EMPL_ID" length="11">
<generator class="native"/>
</id>
<!-- employee -->
<many-to-one name="employee" class="core.Employee"
column="EMPL_ID" cascade="save-update,merge" lazy="false" />
<!-- message -->
<many-to-one name="message" class="core.Message"
column="MESS_ID" cascade="save-update,merge" lazy="false" />
<property name="readFlag" type="character" column="READ_FG" />
</class>
</hibernate-mapping>
Here is my problem :
let's say I already have a message in database, and I want to create a messageEmployee and save it.
Code snippet :
Message sent = new Message(content, title);
Employee e = employeeDao.loadEmployeeWithId(Integer.valueOf(to));
messageDao.merge(sent)
MessageEmployee m = new MessageEmployee(sent, e, null);
m.setReadFlag('N');
messageEmployeeDao.mergeMessageEmploye(m);
When I merge(messageEmployee), it creates a new message and a new messageEmployee in database, but I don't want it to create a new message.
I'm quite sure my mapping is wrong since I am no expert, so what could I change to get the behaviour I want ?
It will create a new Message object because you are not providing id in your message object sent. When you execute messageDao.merge(sent), the sent object doesn't have and id to merge with. It is pure transient object. Try loading it from database like Employee.
Hello Stack Overflow Experts, i have need of your expertice:
I am trying to use Hibernate on an Existing DB.
Currently im trying to load a User object and a list of UserData objects that go along.
in the DB the (simplified) layout is
| User | | UserData |
---------------- -----------------------------------
uid | username | | uid | parentuid | field | value |
So each User object matches all the UserData objects where UserData.parentuid = User.uid.
My User class mapping file
<class name="com.agetor.commons.security.User" table="ac_users">
<id name="uid" column="uid" type="long" >
<!--<generator class="native"/>-->
</id>
<property name="username" column="username" />
<list name="fieldData" cascade="all">
<key column="parentuid" not-null="true" />
<index column="parentuid" />
<one-to-many class="com.agetor.commons.fields.FieldData"/>
</list>
</class>
Mu UserData mapping file
<class name="com.agetor.commons.fields.FieldData" table="ac_userdef_data">
<id name="uid" column="uid" type="long" >
<!--<generator class="native"/> -->
</id>
<!--<property name="parentuid" column="parentuid" /> -->
<property name="fieldname" column="fieldname" />
<property name="value" column="value" />
</class>
So far i have tried many different configurations and all of them have had various degrees of failue. The code pasted here, does not work.
The parentuid property is commented out, because Hibernate gives a "Repeated column in mapping" error otherwise.
Currently there is still a "Repeated column in mapping" on the uid field, i use for <list-index />
I do not understand where i specify that UserData.parentuid is the foreign key and that the list should use User.uid as key.
I hope someone is able to help.
When you define both a One-To-Many and a Many-To-One, does this not make it Bi-Directional?
The current working model, is Unidirectional and UserData does not have a reference to User. Your suggestion fails, because Hibernate could not find a get or set method for User on UserData.
Is it implied that, this code uses User.uid as a key and matches this against the UserData.parentuid column? Or is this fact specified somewhere else?
<list name="fieldData" inverse="true">
<key column="parentuid" not-null="true" />
<one-to-many class="com.agetor.commons.fields.FieldData"/>
</list>
I am still learning Hibernate and working my way through documentation and examples i can find.
Try this (drycode):
<class name="com.agetor.commons.security.User" table="ac_users">
<id name="uid" column="uid" type="long" >
<!--<generator class="native"/>-->
</id>
<property name="username" column="username" />
<list name="fieldData" inverse="true">
<key column="parentuid" not-null="true" />
<one-to-many class="com.agetor.commons.fields.FieldData"/>
</list>
</class>
<class name="com.agetor.commons.fields.FieldData" table="ac_userdef_data">
<id name="uid" column="uid" type="long" >
<!--<generator class="native"/> -->
</id>
<many-to-one name="user" class="com.agetor.commons.security.User" fetch="join">
<column name="parentuid" not-null="true"/>
</many-to-one>
<property name="fieldname" column="fieldname" />
<property name="value" column="value" />
</class>
If it works, try adding the index and cascade stuff that I left out.
cheers,
mitch
I think you're close, but List is probably not the data structure you want in your new User class. A list implies an ordering of the child elements, which the tables don't seem to have. If you have an indexed collection like a list, the data has to have a separate column that provides the index, hence the "repeated column in mapping" issue.
The rules of thumb I use are - each mapping should include an entry for each member of the class it is mapping. So for instance your User class should have an entry for its collection of FieldData, but your FieldData mapping does not need an entry for parentuid, since that column doesn't map to an element of the FieldData class - it's just used to put the FieldData within the collection in the parent object.
If the User object really does contain an ordered collection of FieldData, then use a set instead. On the other hand, I think it's more likely you'd want a map of FieldData instead, using the 'field' name as the index. Then the FieldData class doesn't need to map the 'field' column and won't have that as a member, it'll just have the value (and any other fields from this table - of course if there really is only one column left to map, you might just end up with a map of strings to strings.
This is the latest development on this problem. This configuration can succesfully load from the Database. Saving does not work.
I have decided to alter the Database design instead, so im posting this here for reference for others, before i abandon this approach.
<class name="com.agetor.commons.security.User" table="ac_users">
<id name="uid" column="uid" type="long" >
<generator class="increment"/>
</id>
<property name="deleted" column="deleted" />
<property name="username" column="username" />
<property name="password" column="passwd" />
<property name="disabled" column="disabled" />
<property name="lockout" column="lockout" />
<property name="expires" column="expires" />
<bag name="fieldData" lazy="extra">
<key column="parentuid" not-null="true" />
<one-to-many class="com.agetor.commons.fields.FieldData"/>
</bag>
<bag name="groups" table="ac_group_rel" access="field" lazy="extra">
<key column="useruid"/>
<many-to-many column="groupuid" class="com.agetor.commons.security.Group"/>
</bag>
<join table="ac_userdef_data" optional="true" fetch="join">
<subselect>
select
*
from
ac_userdef_data data
where
data.objectname = 'user' and
data.fieldname = 'firstname'
</subselect>
<key column="parentuid" />
<property name="firstname" formula="(select data.value from ac_userdef_data data where data.fieldname = 'firstname' and data.uid = uid)"/>
</join>
<join table="ac_userdef_data" optional="true" fetch="join">
<subselect>
select
*
from
ac_userdef_data data
where
data.objectname = 'user' and
data.fieldname = 'lastname'
</subselect>
<key column="parentuid" />
<property name="lastname" formula="(select data.value from ac_userdef_data data where data.fieldname = 'lastname' and data.uid = uid)"/>
</join>
</class>
-->
<list name="fieldData" cascade="all">
<key column="uid" not-null="true" />
<index column="parentuid" />
<one-to-many class="com.agetor.commons.fields.FieldData"/>
</list>
Try this , i just changed key coloumn as parentuid to uid.(And better use set in hibernate mappings)
cheers,
Nagendra Prasad..
I have a Master Detail relationship configured. The hbm file is below. When I run some code like this
Favourite favourite = favourites.Find(f => f.Id== id);
user.Favourites.Remove(favourite);
m_UserRepository.Save(ref user);
I get the error message
NHibernate.Exceptions.GenericADOException: could not delete collection rows: [Model.Entities.User.Favourites#249][SQL: SQL not available] ---> System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'UserId', table 'BE.Favourite'; column does not allow nulls. UPDATE fails.
Any suggestions on what this means please help.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Model.Entities" schema="BE" assembly="Model" default-lazy="false">
<class name="Model.Entities.User, Model" table="Users" >
<id name="UserId" column="UserId" type="int" unsaved-value="0">
<generator class="native" />
</id>
<property name="UserName" column="UserName" type="string" />
<bag name="Favourites" cascade="all" lazy="true">
<key column="UserId"/>
<one-to-many class="Model.Entities.Favourite, Model"/>
</bag>
</class>
</hibernate-mapping>
Have you tried setting inverse="true" on your bag?
You actually need a many-to-many relationship in this case:
<class name="User">
<id name="Id">
<generator class="native">
<param name="sequence">object_sequence</param>
</generator>
</id>
<version name="Version" />
<property name="Name" />
<set name="Roles" table="User_Favourite">
<key column="UserId"/>
<many-to-many column="FavouriteId" class="Favourite"/>
</set>
</class>
And the same on the other side: (*note the inverse="true")
<class name="Favourite">
<id name="Id">
<generator class="native">
<param name="sequence">object_sequence</param>
</generator>
</id>
<version name="Version" />
<property name="RoleName" />
<set name="Users" table="User_Favourite" inverse="true">
<key column="FavouriteId"/>
<many-to-many column="UserId" class="User"/>
</set>
</class>
From the NHibernate 2.0 Documentation:
Very Important Note: If the <key> column of a <one-to-many> association is declared NOT NULL, NHibernate may cause constraint violations when it creates or updates the association. To prevent this problem, you must use a bidirectional association with the many valued end (the set or bag) marked as inverse="true". See the discussion of bidirectional associations later in this chapter.
Finally, I'm unsure if you really want to use a bag here. One user can have the same favorite two or more times?
P.S.: Also, note that lazy="true" is the default behavior since NHibernate 1.2.
try changing your cascade rule to:
<bag name="Favourites" cascade="all,delete-orphan" lazy="true">
<key column="UserId" not-null="true"/>
<one-to-many class="Model.Entities.Favourite, Model"/>
</bag>