0

You can have OneToMany relation between 2 tables, but You can also have this relation in just one table. So, in this case, List of some property (column) will be repeated.

Let say, we have Entity with column ID_FIRST_TABLE and list of values ID_SECOND_TABLE for that ID_FIRST_TABLE. As object it will be object1.listObjects2, and if it can be configured with just one table, it will be simple objectRepository.save(object1) and it will store

GENERATED ID | ID_FIRST_TABLE | ID_SECOND_TABLE | NAME
      1                1              1           Name1
      2                1              2           Name2
      3                2              3           Name3
      4                2              2           Name2

In this ONE TABLE is object (FIRST_TABLE) which has property as list of other objects (SECOND_TABLE).

Aobject1(FIRST_TABLE)
                    .id=1
                    .List<SECOND_TABLE> -> Bobject1(SECOND_TABLE)
                                                                 .id=1
                                                                 .name=Name1
                                        -> Bobject2(SECOND_TABLE)
                                                                 .id=2
                                                                 .name=Name2

Aobject2(FIRST_TABLE)
                    .id=2
                    .List<SECOND_TABLE> -> Bobject3(SECOND_TABLE)
                                                                 .id=3
                                                                 .name=Name3
                                        -> Bobject2(SECOND_TABLE)
                                                                 .id=2
                                                                 .name=Name2

So, I want to have just one table where when I save Aobject1 will save 2 records in that table (GENERATED ID 1 and 2). When I save Aobject2, it will save also 2 records in that table (GENERATED ID 3 and 4).

user1182625
  • 145
  • 2
  • 15
  • It is not at all clear exactly what you are asking. – Alan Hay Feb 19 '20 at 08:58
  • I explained little be more detail – user1182625 Feb 19 '20 at 12:55
  • If you use Mongodb instead of MySQL then you'll be able to do this easily. Try a NoSQL database which does exactly what you are asking for. – dxjuv Feb 20 '20 at 13:09
  • @ROHAN, I am using Oracle – user1182625 Feb 21 '20 at 22:24
  • You can not do that in Relational databases. You'll have to create different tables if you have mapping in your code. That's just how RDBMS databases work. – dxjuv Feb 24 '20 at 04:17
  • @ROHAN, as far as I know, in RDBMS you can have relation inside one table. I just want to know how can I achive this with configuration. I know I can achive to have entity at lowest level with all properties up to all parent classes. I just want to know can I achive the same with just relations through properties and to have One2Many in just one super table. https://stackoverflow.com/questions/9774715/mysql-multiple-tables-or-one-table-with-many-columns – user1182625 Feb 24 '20 at 14:16

0 Answers0