I have xml files containing data that I wish to insert into my database using Nhibernate. I can write some Linq that will process my data and map it to my nhibernate objects but I was wondering if there was some way of doing this without the need to write the translator from XML to Nhibernate myself.
Am not sure if something exists along these lines or if anyone has written something in the past to get XML data into the database using NHibernate.
Thanks for any suggestions!
I believe this is what your looking for:
http://markmail.org/message/3jkwktw6226357qs
Related
I am using VS 2010, SQL 2008 with nhibernate to perform database operations.
I know that we can use hbm.xml file to join 2 or more tables. I want to know how to make use of this file in retrieving the data by using the joins specified in this file?
Pls help me. Or any link which gives me info on this.
Thanks,
Pavan
It sounds like you need to read over the nhibernate documentation. The first few sections give a rough overview of how hbm files map to POCOs (objects) and your database.
You should also read the Getting Started Guide on nhibernate.info. These will answer your somewhat vague question above.
Is it possible to generate a schema of a database from nHibernate, where I have provided nHibernate with the configuration to the database but I have not written any mappings.
I wish to get the Database MetaData/Schema programmatically.
I am using an Oracle Database. I have tried two approaches:
Approach one:
public DatabaseMetadata GetMetadata(DbConnection connectionIn)
{
return new DatabaseMetadata(connectionIn, _dialect);
}
Problem: This seems to be what I need however, although it correctly connects, it hasn't picked up any of my tables. All I provided was the nHibernate Configuration object which was populated with the contents of my nHibernate.xml.config file (connection string, driver client, etc).
Question: Why would it not return the table data? It's connected correctly but finds nothing!
Approach two:
public void DatabaseSchema()
{
var schema = new SchemaExport(nHibernateConfiguration);
schema.SetOutputFile("schema.dll");
schema.Create(true, true);
}
nHibernateConfiguration is an instance (property on class) of the nHibernate Configuration object, populated with contents from the nHibernate.xml.config class.
Problem: This simply doesn't work. Crashes with the following exception:
NHibernate.MappingException : Dialect
does not support identity key
generation
I suspect this will only generate a schema based on mappings you have created? I have created no mappings. The idea is this will work against whichever database I have connected to a generate a schema for it.
Question: Is my belief that this method will only generate a Schema based on my mappings? If not, Am I using it correctly?
Hopefully this is clear enough, comment if I need to provide more info.
Thanks In Advance.
To be clear: I have a database and want to get meta data representing the database, a schema.
NHibernate is actually based on the mapping files. You could generate classes or tables from them. There are tools to generate the mapping files, but they are based on the classes, not the tables.
Answers to your specific questions:
Approach one: NHibernate does not read table definitions from the database. All the table definitions need to be specified in the mapping files.
Approach two: SchemaExport creates an SQL file (Create tables, indexes etc) from the mapping definitions. It is actually recommended to use it, unless you need to cope with legacy databases. The output file should be called *.sql, not *.dll.
The error you get is most probably because you try to create an identity id on an oracle database (or another which does not support identity columns). Use hilo instead (or, if you don't like it, guid.comb or native). I just wonder why you get this error, I thought that you didn't write any mapping files?
Conclusion:
I don't know of any tool which create NHibernate mapping files from database tables. There may be one, most probably it is not free or not mature (because otherwise it would be well known). So I suggest to think about generating the table definitions instead, or, in case you have a legacy database, you need to go through writing the mapping files manually.
There are several tools to help you out but the two I use the most are the following two.
NHibernate Schema Tool
NHibernate Mapping Generator
If you already have a schema you can use the NHibernate Mapping Generator to create your mappings. You can then use the mappings for whatever you want. Modify them and use NHibernate Schema Tool to manage the actual schema.
If you don't have any schema and that is what you are trying to create you are on the right track. First you need to "map" your classes. Preferably using Fluent NHibernate or ConfORM like Michael Maddox suggested.
I don't know the purpose of this. If it is database schema management I would recommend against using NHibernate. NHibernate was never developed as a schema manager tool so it probably should not be used this way. Admittedly I might have misunderstood you somehow and this answer could be completely wrong.
I may be interpreting the question wrong, it's not really clear what you are asking for.
Assuming you have created classes and configured NHibernate correctly and you want to create tables in the database for those classes, you have at least two potential ways to try to generate a database without creating NHibernate mappings, both of which will likely work much better with at least some hints about how to do the mappings:
Fluent NHibernate Automapper
ConfORM
There is a decent learning curve for both options.
Another option is to try one of the commercial visual designers for NHibernate, although those tools aren't quite mature enough to do this really well in my experience.
Core NHibernate is not designed or intended to create tables without mappings files.
Simple question, hopefully with a simple answer. I was under the impression I should create my tables and then use the map files to map my POCO classes to the database objects. After following some tutorials, I have this code in my Test SetUp:
new SchemaExport(_configuration).Execute(false, true, false, false);
Which, obviously, create the table structure as defined in the mappings, so I'm confused.
You need the mapping files for SchemaExport.
Ideally you write / design your classes first, write the mapping files afterwards. When writing mapping files, you are designing the database tables at the same time. SchemaExport just creates the schema from the mapping files where all the information is usually available.
SchemaExport is highly recommendable, even if it is a bit "hidden" in the docs.
It is better to have only one place to change, in your case you make changes in your classes and your db is automagically updated - do you expect anything better?
Sometimes you write new code for existing database, so you have to create (or have) database first, then need to prepare mappings manually.
EDIT:
I am using Fluent NHibernate, so this can be done fully automatically, not sure in different environments.
We are using NHibernate as our ORM framework.
We have a need to persist classes we load at run time. We do that according to metadata they come with, that holds names and types of the data they have.
In order to build the tables for them at run time, we use the SchemaExport class from the NHibernate ToolSet API.
We wanted to ask two questions:
Is there a way to make NHibernate do all the actual creations in one roundtrip to the DB instead of a roundtrip per table?
In order to use the SchemaExport tool are building a dynamic string that represents a mapping file from a template we keep. Is there a better way to do this? Maybe even without a mapping string?
Ad 2.
If I understand you correctly, you don't want to use hbm mappings, right? Have you considered using Fluent NHibernate? (http://fluentnhibernate.org/)
I have a table in oracle having column of datatType SYS.XMLTYPE and have to map it in Nhibernate hbm to retrieve the XML. I am using C# with .net framework 3.5 is there any specific dataType available to map this or can i use byte[] or char[] for mapping?
Thanks
Arvind
I think the best approach would involve creating an NHibernate UserType, although you might be able to get read only access using a formula approach and mapping as a clob type.
There is an example of doing this in Hibernate that might be easy to port across to .net (see also this forum post and this blog post)
Generally I find it useful to search for solutions to these kind of problems using a keyword of "Hibernate" as well as "NHibernate" for these issues as the solutions are often easily portable to NHibernate
use XMLDOC...it works like charm