Is there any way to get Nhibernate mapping to perform a join between a child and parent tables
I have a product table and a product group table. there is a key between these tables of GroupId. When i use a join in mapping for a Product it tries to Join on the ProductId to the GroupId instead of the GroupId to GroupId.
Is there no easy way to do this?
Your mappings are probably wrong.
If Product has a reference (FK) to Group, it should be mapped as:
<many-to-one name="Group" column="GroupId"/>
If that's not the case, please post your classes.
Is the foreign key set up in your database? If not add it in the database and try including it in the reference in your Nhibernate Product mapping:
e.g.,
<many-to-one name="Group" column="GroupId" foreign-key="FK_Product_ProductGroup" />
Note: foreign-key value there is just a guess of what it would be called, grab it from the database properties :)
Related
<many-to-one name="attachment" class="AttachmentEntity" lazy="false"
fetch="select" cascade="delete">
<column name="SPA_ATTACHMENT_ID" not-null="true" unique-key="IDX_AMT_COND_01"/>
</many-to-one>
What is the Unique Key doing and how will it work as a string?
As per the JBoss documentation,
A unique-key attribute can be used to group columns in a single,
unique key constraint. The attribute overrides the name of any
generated unique key constraint.
Typical use case for unique-key would be, when you want the values of multiple columns as a whole to be unique.
For example:
class Department {...}
class Employee {
Integer employeeId;
Department department;
}
So, to ensure that 2 Employee objects with same employeeId and department are not persisted, we can use the unique-key attribute with same value EmpIdDept on the 2 columns EMP_ID and DEPT_ID to enforce the uniqueness constraint on them as a whole:
<property name="employeeId" column="EMP_ID" unique-key="EmpIdDept"/>
<many-to-one name="department" column="DEPT_ID" class="Department" unique-key="EmpIdDept"/>
The string specified as the attribute value, i.e. IDX_AMT_COND_01 in your case, is just the name of the multi column unique constraint.
Also check this answer and this one (to achieve the same using #UniqueConstraint)
NOTE: to use single column unique constraint, you need to use
unique="true"
Hello I have 2 entities that are CUSTOMER and PRODUCT at my Entity Relation Diagram(ER).
CUSTOMER and PRODUCT has a M to N relationship which is RATE and this relationship has 2 attributes which are Comment and Rate.
My PRODUCT entity has a derived attribute named Rating-avg which is the average rating of the product, being rated by the CUSTOMER's.
I don't know and can't find how to add the derived attribute to the table while creating it or altering it.
I would be really glad if someone could help.
I am using SQLite3(3.25.2) and SQLiteStudio(3.2.1) (The latest versions up to date.).
You would use a third table, which is called a "junction" or "association" table:
create table CustomerProducts (
CustomerProductId int primary key,
CustomerId int references customers(CustomerId),
ProductId int products(productId),
Rate ?, -- unclear what the type is
Comment text
);
You could name the table Rate, if you like. I typically name association tables after the two tables involved in the relationship, unless it is an entity itself.
My database structure mainly consists of multiple primary keys per table, therefore multiple columns are required per join. I'm trying to use ColdFusion (11 to be specific) ORM relation property that has a linktable. I think the issue is with two keys on one side of the many-to-many relationship and just one on the other. Here is what I've tried without success:
Table Setup
Staff StaffSites Sites
============ ============ ===========
YearID (PK) --- YearID (PK)
StaffID (PK) --- StaffID (PK) SiteName
StaffName SiteID (PK) --- SiteID (PK)
Staff ORM CFC
component persistent=true table='Staff' {
property name='id' column='StaffID' fieldType='id';
property name='year' column='YearID' fieldType='id';
property name='name' column='StaffName';
property name='sites'
cfc='site'
linkTable='StaffSites'
fkColumn='YearID,StaffID'
inverseJoinColumn='SiteID'
mappedBy='id'
fieldType='many-to-many'
lazy=true;
}
Site ORM CFC
component persistent=true table='Sites' {
property name='id' column='SiteID' fieldType='id';
property name='name' column='SiteName';
}
ColdFusion Error
collection foreign key mapping has wrong number of columns: staff.sites type: integer
If the join table has multiple foreign key columns (that reference the
composite key of the target table), then you must use comma-separated
column names.Also, the order in which the column names are specified
must match the order of composite keys defined.
http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WS5FFD2854-7F18-43ea-B383-161E007CE0D1.html
Composite key should be supported.
Try switching the id's since you define staff id first, and also specify the orm type on the id's just in case. Also, try swapping inverseJoinColumn with fkColumn, I never remember which should be which but that'd be something I'd try.
I have two tables, say:
PAYMENT
------------------------------
OrderId INT PK
PaymentId INT PK
Amount FLOAT
ChildPaymentRowNum INT
CARD_PAYMENT
------------------------------
OrderId INT PK
PaymentRowNum INT PK
CardType STRING
CHEQUE_PAYMENT
------------------------------
OrderId INT PK
PaymentRowNum INT PK
CheckNumber INT
No, I didn't make this DB and no I can't change it. I want to map CARD_PAYMENT and CHEQUE_PAYMENT as joined-subclasses of PAYMENT. The difference in this model from the examples I've found is that I'm both using a composite key and one of the column names in the foreign table doesn't match.
I think if it were not a composite key I could do this:
<joined-subclass name="CardPayment" table="CARD_PAYMENT" extends="Payment">
<key column="PaymentRowNum" foreign-key="ChildPaymentRowNum">
</joined-subclass>
And if the names matched on the composite key I could do this:
<joined-subclass name="CardPayment" table="CARD_PAYMENT" extends="Payment">
<key>
<column="OrderId">
<column="PaymentRowNum">
</key>
</joined-subclass>
But, while I'd like to do something like this I'm pretty sure it's illegal:
<!-- NO GOOD -->
<joined-subclass name="CardPayment" table="CARD_PAYMENT" extends="Payment">
<key>
<column="OrderId" foreign-key="OrderId">
<column="PaymentRowNum" foreign-key="ChildPaymentRowNum">
</key>
</joined-subclass>
So, how would I do something like this?
BONUS POINTS: if you can tell me how to do it with NHibernate.Mapping.Attributes, but if not I can probably figure it out.
The foreign-key attribute is used to specify the name of a foreign key constraint (not column!) that should be created by the NHibernate schema generation tool. It has no affect on NHibernate during runtime.
Why do you want to specify the key column name of the base class in the subclass key? Even the NHibernate documentation for joined-subclass uses different column names in both tables: http://nhibernate.info/doc/nh/en/index.html#mapping-declaration-joinedsubclass
I'm not sure how best to phrase the question, but essentially I have a table of contacts, and instead of doing the typical -- a contact has a reference to a table with spouse information, and a table with children, I want each of those people to be a contact, but then define a relationship between those contacts (brother, sister, child, spouse, etc.). So the contacts would exist in a single table, but I'm having trouble determining how best to define the relationship based upon their contact id and the relationship type. Any advice would be appreciated.
CONTACTS table
contact_id, pk
CONTACT_RELATIONSHIP_TYPE_CODE table
contact_relationship_type_code, pk
description
CONTACTS_RELATIONS table
parent_contact_id, pk, foreign key to CONTACTS table
child_contact_id, pk, foreign key to CONTACTS table
contact_relationship_type_code, foreign key to CONTACT_RELATIONSHIP_TYPE_CODE table
If you see the need to support multiple relationship types to a pair of people, add the CONTACTS_RELATIONS.contact_relationship_type_code column to the composite primary key
This is called a self join, it is pretty common and fairly easy to provide the functionallity you mention above. Take a look at this article.
Just implement an intersect table with four columns - key, contactid #1, contact id#2, and relationship.
Why do it this way? Because a contact can have several relationships.