EclipseLink class and xml mix mappings - eclipselink

I wanted to mix class based JPA configuration with using annotations together with xml based configuration and it works fine in terms of Dynamic Fields Extensions - I can add to xml fields that are not defined in class and they are being used fine.
The question is how to do that with mappings - I wanted to add OneToOne or OneToMany mapping to the xml and have that working but no luck at the moment. Is that even possible to add for existing class new mapping to new class, in xml orm file?

Related

How to create dynamic NHibernate mappings without generating HBM XML files?

I'm working on a dynamic application with NHibernate. My goal is to create dynamic entities (both class type and mapping xml file) based on some data. For example suppose I want to create a Person entity dynamically in runtime and on the fly.
I use Reflection.Emit to generate class type dynamically. For creating mapping dynamically I used Ayende's code.. But unfortunately this code does not work because mappings does not have Classes property. I tried to code as same as codes of Castle ActiveRecord and Fluent NHibernate but they generate HBM XML files. As I don't want to generate/create mapping files so I can not use these solutions.
Is there any way that like Ayende's solution not to be forced to generate HBM XML mapping files and just doing everything dynamically and in memory?
fluentnhibernate creates hbm in memory just to feed them to nhibernate. fluentnhibernate has the nice automapping feature with costumizable conventions, perfect for this situation. Also in FNH 2.0 they are working to skip hbm for better performance, but normally you'll never see the mappings outside memory.
Sample:
Assembly assembly = GetDynamicallyCreatedTypesAssembly();
ISessionFactory sf = Fluently.Configure()
.Database(...)
.Mappings(m => m.AutoMappings.Add(AutoMap.Assembly(assembly)))
.BuildSessionFactory();
NHibernate 3.2 has a mapping-by-code layer that does what you want.
I'm not sure if dynamic classes will work, but it doesn't hurt to try.

Nhibernate Mappings

Is there any way to get references to the mapping objects that NHibernate creates from the XML files? How about the ClassMap objects that FluentNhibernate creates? I wanted to create some query generation functions (for row counts, etc.) using this information. Since I went through the trouble of mapping it, I ought to have access to it in code, right?
Actually, Fluent ClassMaps are translated to XML.
The ISessionFactory exposes a GetAllClassMetadata method that is probably what you need.

NHibernate (not Fluent NH) - Is it possible to reuse components?

Is there any way to reuse NHibernate components (<component>) in more than one mapping?
For example: an Address class in Employee and Customer classes - The only way to do this which I see now is to copy-paste the Address component mapping from one of the class mappings into another.
I haven't tried this with NHibernate but it worked with another OR Mapper that used XML configuration files. Create the component mapping in it's own XML file, then in the XML file that uses the component use an XML external entity to reference it. Here's a snippet from my Company mapping file:
<!DOCTYPE mappings [
<!ENTITY Address SYSTEM "MyCompany.MyApplication.Mappings.Address.xml">
]>
<mappings version="4.2" defaultNamespace="MyCompany.Model" defaultSchema="dbo">
<entity type="Company" table="Company" keyMember="CompanyId" keyType="Auto">
&Address;
Apart from the solution that Jamie proposed, using code-based mappings provides better possibilities for reuse.
Currently, there are two such projects, Fluent NHibernate and ConfORM.
Unfortunally not. I really need that feature too ;)

NHibernate: completely overriding base domain entity

I have a situation where I have a Common.Domain.Person and Specific.Domain.Person.
First one should be provided as a part of a common package.
Second one appears when common package has to be customized to fit the needs of specific project.
In the object model, it can be easily implemented with inheritance.
In the NH mapping, however, I have encountered a small problem.
I can create an NHibernate <subclass> mapping, but that would require me to use an discriminator. However, I know that if specific person class was inherited, then common class instances will never be used within this specific project.
What is the best way to implement this without adding discriminator column to the base class (since there are no different cases to discriminate)?
this is what i wanted and nhibernate supports it using xml entities. Unfortunately this feature has been borked since (at least) NH v2++.
see also Using Doctype in Nhibernate
A work-around could be to inject these properies programmaticaly when you create the SessionFactory (Dynamic Mapping)
see also http://ayende.com/Blog/archive/2008/05/01/Dynamic-Mapping-with-NHibernate.aspx
Just map the Specific.Domain.Person and leave Common.Domain.Person unmapped.
If you are not saving instances of it, NHibernate does not need to know about it.

Nhibernate question about extension object without changing the original mapping

Say i have a cms with only one object called Article (for the sake of this example) with an ID and a title. This CMS implementation becomes part of a framework and is used as a library: e.g. CMSFactory.CMS.SaveArticle(a);
The problem is that depending on the project requirements an article object may have more fields such as SomeDate. Is there any way to declare this relationship and still save an article with all its extra (project-dependent) fields without changing the base CMS library (but able to declare new mappings or so)?
You could create a subclass of Article with the specific fields for the project:
class SpecialArticleForThisProject: Article {
public DateTime SomeDate {get;set;}
}
And map SpecialArticleForThisProject using one of the three inheritance mapping strategies.
The base CMS library wouldn't require any changes.