Orm discriminator-column of the parent table must refer to a value which is of a different table - jpa

The question is quite confusing so I'll describe in detail as follows.
I have a domain object User (which has its respective User table in database). I have another domain Investor (which has its respective Investor table in database) that extends User. Now a User can be a Investor if User's userType='I'
<entity class="User" >
<table name="domain.Users" />
<discriminator-column name="user_type" discriminator-type="STRING"/>
</entity>
I need to map the user_type to its respective domain and tables.
<entity class="domain.Investor" >
<table name="domain.Users" />
<discriminator-value>I</discriminator-value>
</entity>
In the above code i cannot change the table name to the database's Investor table because the discriminator value is for User's table. I want the xml code where discriminator's column of Users table is I but it should have its own table Investor.

Investor should be able to use both the User and its own Investor table in a joined table inheritance structure if that is what you need. This will work out such that each row in User represents a User entity or User subclass, while each User that is really an Investor will also have a row in the Investor table. See http://wiki.eclipse.org/EclipseLink/Examples/JPA/Inheritance for a simple example of how to set up inheritance - you are missing the <inheritance strategy="JOINED"/> tag in User that specifies the JPA inheritance type to use.

Related

How to map in JPA a field that can hold a foreign key to on of two tables?

I have the following DB tables: Teacher and Student. They contain a few common fields I would like to hold in my java code in a Person class, which will be the parent class of my Teacher and Student entities. I also have an Event table, that contains an organizer field. The organizer field contains the id of either a Teacher or a Student.
The question is, can this be mapped in a way that the event contains a Person type as organizer? If not, what other options do I have?
Of course, there are plenty of solutions that require me to query the organizer separately from the event, I would like to avoid that.

What's the relationship between res.partner and res.user?

I am new to odoo v8 and i am not able to understand the relationship between res_partner and res_users tables and also with hr_employee table are they all related?
The relationship between res.partner and res.user is that res.user inherits from res.partner using an inheritance type called "Delegation Inheritance" (see documentation).
Because of "Delegation Inheritance" every res.user record has a mandatory internal connection to a corresponding res.partner record using a field partner_id. What is this connection all about is to directly use all the fields of res.partner to store data shared by res.user and res.partner (i.e. name, phone, etc... if for example you refer to phone property of a record of res.user you'll get the value stored in the corresponding res.partner record) so res.user has to define fewer number of fields on it's own, like password, login, etc..
Note also that because of this relation res.user can NOT exist in the system without corresponding res.partner, it's why every res.user has one, but nonetheless res.partner can exist without res.user.
hr.employee have m21 with res.users (user_id)
res.users have m21 with res.partner(partner_id)
Actually only res.users has a "real" relationship to res.partner, because with every user odoo will create a partner (per default no customer and no supplier). this partner will be used e.g. for emails and the followers system in odoo.
But you can have partners without users, too. That will be a normal partner, for defining customers and suppliers.
And finally there is the employee. You can set a user on it. If i recall right, the user will be used for attendances and timesheets.

ORM to create single entity from more than one database tables

Well tested running system have already defined entity called 'User'.
Now I need to add a new property to User entity (ex: Age)
To do this in the safe way, I do not like to do any changes with the existing data base table, because that is very risky in my case. I need a way to rebuild the User entity with the minimum code changes.
So my proposal is:
Create a new table (user_age), with two columns (user_id, age)
Modify the user entity to add property 'age' and its getter-setters
So my entity (User) properties, will be saved to two different tables (user and user_age)
Loading the user is also similarly.
Is this possible to do with hibernate....??
If not, Any other safer way to do this with Hibernate...?
what are the available ORMs that provide this kind of feature (nhibernate, entityframwork,etc... or any other ORM)...?
Yes, there are various approaches:
[1] See JPA Secondary Tables. This allows you to map an Entity to two or more tables.
Section 2.2.7: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e2235
[2] Create another Entity, say UserInfo, mapped to this new table. Create a one-to-one mapping from User to UserInfo.
Yes. You can do that.
I've used for a similar problem a joined-subclass.
Base:
<class name="User" table="Users">
<id name="Code" type="System.Guid">
<column name="Code" />
<generator class="guid.comb" />
</id>
...
</class>
Subclass:
<joined-subclass name="UserExt" extends=User" table="UsersExt">
<key column="Code" />
<property name="Age">
<column name="Age" not-null="true" />
</property>
</joined-subclass>
A good reference here.
NHibernate's join mapping is for exactly this case.
See Ayende's blog and the documentation for more information. From the documentation:
Using the <join> element, it is possible to map properties of one class to several tables, when there's a 1-to-1 relationship between the tables.
From my searches, it looks like it is also possible to do this with Entity Framework: Simon J Ince - Mapping two Tables to one Entity in the Entity Framework . I think this article is about Entity Framework v1, and things could have changed by now, but it appears that there is an important limitation in Entity Framework's version of this mapping:
... it requires a record in each table to exist as the generated SQL uses an INNER JOIN. It makes sense if you're using a new model, but I guess this is more tricky if you're mapping to an existing schema and data.
With NHibernate, you can set the optional attribute on the join mapping to tell it to use outer joins instead of inner joins.
optional (optional - defaults to false): If enabled, NHibernate will insert a row only if the properties defined by this join are non-null and will always use an outer join to retrieve the properties.

NHibernate duplicate insert in many-to-many and composite-element

I have in an NHibernate 2 application an Product entity which has an many-to-many relationship to an Location. The Location entity does not have any navigation properties or mappings back to the product. It is mapped like this:
<bag name="Locations" table="ProductLocation" cascade="none">
<key column="ProductId" />
<many-to-many column="LocationId" class="Location"/>
</bag>
The product also has an composite-element, a Component with a concentration mapped via the ProductComponent class. The class has no navigation property or mapping back to the product.
<bag name="ProductComponents" table="ProductComponent" access="nosetter.camelcase">
<key column="ProductId" />
<composite-element class="ProductComponent">
<property name="Concentration"/>
<many-to-one name="Component" column="ComponentId" access="nosetter.camelcase"/>
</composite-element>
</bag>
This all works fine when just inserting one product at a time. It however fails when batch inserting multiple products.
While the products itself get inserted fine, each product does get an own unique Id, the elements in the many-to-many (Locations) and composite-element (ProductComponent) doesn't get inserted well. This is because NHibernate multiple times executes the insert to the ProductLocation table with the same ProductId.
This causes an duplicate record in the link table. How can this be prevented?
You'll have to define one site of the relationship to be the owner so that only one side does the insert. This can be achieved with Inverse set to true on the other side.
Find a more detailed explanation here

Create Entity of specific columns

I am using Breeze (http://www.breezejs.com/) and to use the functionality I want it requires mapping to a complete entity and all of its fields. I have a "Person" entity, but it includes a Social Security Number field. I want to keep this SSN# field private so I would like to create an entity named SubSetPerson that is updateable, has navigation properties and only contains the columns I want (e.g. ID, FirstName, LastName, myNavigationProperty) and does not contain the SSN#. I am using database/model first. Is this possible?
If you are using database first, then you could create a view for that table which only selects the columns you want. Then update the EF model browser to include that view.
Try using a Master-Detail type structure for your person. The master table would contain the person's public information; ie name, birthdate, etc... The detail table would contain only the more sensitive information (SSN, etc...). Then depending on your needs you can load the detail or not.