kundera icon indicating copy to clipboard operation
kundera copied to clipboard

Many To One Relationship (Unidirectional)

Open Gargee opened this issue 13 years ago • 15 comments

Hi, Now i have this problem which i cant find a solution . I have two tables :- CREATE TABLE "MediaCategoryGroup" (MCategoryID int, MCategoryName varchar, PRIMARY KEY(MCategoryID)); and CREATE TABLE "MediaCategory" (MCategoryID int, submcategoryname varchar,PhotoRankID int, VirtualTourID int, LangID int,mcategoryid2 int, PRIMARY KEY(MCategoryID,LangID, PhotoRankID, VirtualTourID));

I am trying to get a one to many relationship where in many entries in the MediaCategory table can have the same MediaCategoryGroup.

Now only problem here is that the MediaCategory table has a composite primary key of which one is the Primary key in the MediaCategoryGroup table. When i remove this constraint my code works fine ..but when i dont i get this error:- Exception in thread "main" com.impetus.kundera.KunderaException: com.impetus.kundera.KunderaException: InvalidRequestException(why:Missing mandatory PRIMARY KEY part mcategoryid) 2013-06-10 13:46:06,081 [ERROR] [main] com.impetus.client.cassandra.pelops.PelopsClient - Error during persist, Caused by: InvalidRequestException(why:Missing mandatory PRIMARY KEY part mcategoryid)

I have uploaded the code in :-https://github.com/Gargee/KunderaPoc/tree/master/src/main/java/com/Kundera/poc

Gargee avatar Jun 10 '13 21:06 Gargee

Can you please try by changing db script as:

CREATE TABLE "MediaCategoryGroup" (MCategoryGrpID int, MCategoryName varchar, PRIMARY KEY(MCategoryGrpID));

CREATE TABLE "MediaCategory" (MCategoryID int, submcategoryname varchar,PhotoRankID int, VirtualTourID int, LangID int,mcategoryid2 int, MCategoryGrpID int,PRIMARY KEY(MCategoryID,LangID, PhotoRankID, VirtualTourID));

See if it works.

mevivs avatar Jun 11 '13 02:06 mevivs

This works but actually MCategoryGrpID and MCategoryID are the same.So this means i need to maintain duplicate Values here.Because one of the Composite Primary key here for MediaCategory is dependent on the primary key of the MediaCategoryGroup Table. Isn't there any other way to solve this problem?

Gargee avatar Jun 11 '13 15:06 Gargee

The way ManyToOne relation works is, say you have Person=>Address with Many To One relationship, so in PERSON_ID will be stored as a non row key column within ADDRESS column family. So it has to be different name other than ADDRESS_ID(means PERSON_ID). Having both with same names would mean that they are shared by primary key(Which is OneToOne i guess), which i think even may not work in tradational RDBMS systems as well. Also, little issue with Kundera is @JoinColumn should map to @Id name of associated entities.

-Vivek

mevivs avatar Jun 11 '13 15:06 mevivs

Additionally, you can have a look at: https://github.com/impetus-opensource/Kundera/tree/trunk/kundera-tests/src/test/java/com/impetus/kundera/tests/crossdatastore/useraddress

for more on this.

-Vivek

mevivs avatar Jun 11 '13 15:06 mevivs

SO i have this scenerio for Eg: Item table {ItemId(PK) ,IteamName} and Inventory table{StoreName ,NumberofItems,ItemId,AMount pk(ItemId,SToreName) } many stores can have the same items here in Inventory Table the primary key is the combination of the Store Id and Item Id . So here what kind of relation needs to be defined? I understand it has to be Many to onw.SO cant the Inventory Table refer to the primary key of the Item table ?

Gargee avatar Jun 11 '13 15:06 Gargee

If i understood correctly, An inventory can have multiple items.

Which means inventory's primary would get stored as now row key within "Item" table. But in your case, as you are defining inventory table with "Composite/Compound primary key"{more than one columns as part primary key}. So such reference does not more of value add and also not supported by Kundera as well.

However, if Inventory table holds a reference to "Item_id", then it should work(which means Item=>Inventory, it is OneToMany).

So, if you don't create a composite key within Inventory table, it should work.

-Vivek

mevivs avatar Jun 11 '13 15:06 mevivs

So here is the case my Business model needs me to create a ColumnFamily whose Primary key is a composite key and each of the elements of the composite key is the primary key of different tables. Eg T1{a,b,c,d,e pk(a,b,c)}

T2{e,b pk(b)}

T3{k,l,c pk(c)}

Is this case supported in Kundera.If so how get this work other than having a duplicate key for each of these items and then replicating the same data for each entry. for a query on the T1 i should be able to fetch the data from T2 and T3 also

Gargee avatar Jun 11 '13 19:06 Gargee

With, T2=>T1 (One To Many) T3=> T1( One To Many)

it should work.

-Vivek

mevivs avatar Jun 11 '13 19:06 mevivs

so referring to my first question again

So when i try persist any data in MediaCategory which is the T1 in my last post I get this error :-

  • Error during persist, Caused by: InvalidRequestException(why:Missing mandatory PRIMARY KEY part mcategoryid)

now i dont want to cerate a dummy field in my MediaCategory table which is the same as mcategoryid like the one

CREATE TABLE "MediaCategory" (MCategoryID int, submcategoryname varchar,PhotoRankID int, VirtualTourID int, LangID int,mcategoryid2 int, MCategoryGrpID int,PRIMARY KEY(MCategoryID,LangID, PhotoRankID, VirtualTourID));

CREATE TABLE "MediaCategoryGroup" (MCategoryGrpID int, MCategoryName varchar, PRIMARY KEY(MCategoryGrpID));

Rather i would like my table to be like this:-

CREATE TABLE "MediaCategory" (MCategoryID int, submcategoryname varchar,PhotoRankID int, VirtualTourID int, LangID int,mcategoryid2 int,PRIMARY KEY(MCategoryID,LangID, PhotoRankID, VirtualTourID));

CREATE TABLE "MediaCategoryGroup" (MCategoryID int, MCategoryName varchar, PRIMARY KEY(MCategoryID));

can we do this in Kundera?

Gargee avatar Jun 11 '13 19:06 Gargee

Considering MediaCategoryGroup as T2 and MediaCategory as T1( One To Many). It should work.

Try setting MCategoryID explicitly with "MediaCategory" entity,(I guess should be easy to fetch this from MediaCategoryGroup object) and then invoke em.persist() over "MediaCategory" object.

See, if it works.I guess, i understood the issue. I will get this fix later to implicitly assign reference value(which are either partition or cluster keys on associated entity).

-Vivek

mevivs avatar Jun 11 '13 19:06 mevivs

Let me know, if explicitly setting the reference key works for you. I will look into this to fix "implicitly assign value" for referenced keys. This is supported in Kundera and looks like an trivial one to fix.

-Vivek

mevivs avatar Jun 11 '13 19:06 mevivs

Ya I am trying setting the value explicitly now.Let me know when we can have the fix .

Gargee avatar Jun 11 '13 19:06 Gargee

Ok. Will post an update on this.

-Vivek

mevivs avatar Jun 11 '13 19:06 mevivs

A fix has been committed for this. Please verify with latest trunk.

-Vivek

Fixed and releasing with 2.6. Please verify. -Vivek

mevivs avatar Jul 06 '13 19:07 mevivs