Hibernate: Mapping Multi-Rows with ElementCollection - Single table no EmbeddableHibernate update table B after table A is updatedMap User, Address, Country with DDDHibernate: map a foreign key that points to unique keyHibernate fetching issue in table which has no primary keyMultiple @ElementCollection mapped to different columns of the same tableHibernate annotation mapping to joined table with composite keyHibernate: Repeated column in mapping for entityError in One to many mapping in hibernate-spring integrationMapping 2 Tables with same structure in HibernateHibernate: OneToMany in the same table

Does the sign matter for proportionality?

Shrinkwrap tetris shapes without scaling or diagonal shapes

How to stop co-workers from teasing me because I know Russian?

What happened to Captain America in Endgame?

How do I use proper grammar in the negation of "have not" for the following sentence translation?

How did Captain America manage to do this?

Stop and Take a Breath!

Any examples of headwear for races with animal ears?

Is DC-DC (24v to 12v) Buck Conversion typically more efficient than AC-DC (110v to 12v) conversion?

Will a top journal at least read my introduction?

Why do Computer Science majors learn Calculus?

What makes accurate emulation of old systems a difficult task?

How do we know that ממחרת השבת means from the first day of pesach and not the seventh?

Error message with tabularx

Examples of subgroups where it's nontrivial to show closure under multiplication?

Can solid acids and bases have pH values? If not, how are they classified as acids or bases?

how to sum variables from file in bash

Please, smoke with good manners

How can I change the color of a part of a line?

Apply MapThread to all but one variable

Killing undead fish underwater

How to verbalise code in Mathematica?

How to pronounce 'C++' in Spanish

The Defining Moment



Hibernate: Mapping Multi-Rows with ElementCollection - Single table no Embeddable


Hibernate update table B after table A is updatedMap User, Address, Country with DDDHibernate: map a foreign key that points to unique keyHibernate fetching issue in table which has no primary keyMultiple @ElementCollection mapped to different columns of the same tableHibernate annotation mapping to joined table with composite keyHibernate: Repeated column in mapping for entityError in One to many mapping in hibernate-spring integrationMapping 2 Tables with same structure in HibernateHibernate: OneToMany in the same table






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Looking for a solution to map multiple student_id rows under the same class_id, here is the table which has 3 fields:



integer id PRIMARY KEY
integer class_id;
integer student_id;


The table can have multiple rows under the same class_id like this:



Table = 'class'
id class_id student_id
1, 1, 1
2, 1, 2
3, 1, 3
4, 2, 1
5, 3, 1


I'm trying to Map this into a Pojo so I can use it as follow:



List<UniClass> uniClasses = session.createQuery("FROM ClassOfStudent", ClassOfStudent.class).getResultList();


I'm trying to solve this problem by using @ElementCollection



Here is my attempt:



@Entity
@Table(name = "class")
public class ClassOfStudents
@Id
@Column("id")
private long m_id;

@Column(name = "class_id")
private long m_classId;

@ElementCollection
@CollectionTable(name = "class", joinColumns = @JoinColumn(name="class_id"))
@Column(name = "student_id")
private List<Long> m_studentIds;

....



Any idea how to make this work?










share|improve this question
























  • this table describes Many-to-Many relations: between what and what? I can't use @OneToMany since it's a single entity: A one-to-many between what and what? I'm trying to experiment with @ElementCollection: a collection of what? Stored where?

    – JB Nizet
    Mar 22 at 18:35











  • collection of 'std_ids' stored at that table I described.

    – Shvalb
    Mar 22 at 18:37












  • Each row of that table has one and only one std_id, stored in the column std_id of that same table. So there is no association at all, even less a collection. All you need is a column of type long.

    – JB Nizet
    Mar 22 at 18:39












  • there are multiple rows with uni_id=1 and std_id = 1,2,3 - please look at the example.

    – Shvalb
    Mar 22 at 18:40












  • So what? There are several persons with the name "John". That doesn't mean that a person, identified by a unique person_id, has several names. A person has just one name. Just like a uni_class identified by an id has just one std_id.

    – JB Nizet
    Mar 22 at 18:42

















0















Looking for a solution to map multiple student_id rows under the same class_id, here is the table which has 3 fields:



integer id PRIMARY KEY
integer class_id;
integer student_id;


The table can have multiple rows under the same class_id like this:



Table = 'class'
id class_id student_id
1, 1, 1
2, 1, 2
3, 1, 3
4, 2, 1
5, 3, 1


I'm trying to Map this into a Pojo so I can use it as follow:



List<UniClass> uniClasses = session.createQuery("FROM ClassOfStudent", ClassOfStudent.class).getResultList();


I'm trying to solve this problem by using @ElementCollection



Here is my attempt:



@Entity
@Table(name = "class")
public class ClassOfStudents
@Id
@Column("id")
private long m_id;

@Column(name = "class_id")
private long m_classId;

@ElementCollection
@CollectionTable(name = "class", joinColumns = @JoinColumn(name="class_id"))
@Column(name = "student_id")
private List<Long> m_studentIds;

....



Any idea how to make this work?










share|improve this question
























  • this table describes Many-to-Many relations: between what and what? I can't use @OneToMany since it's a single entity: A one-to-many between what and what? I'm trying to experiment with @ElementCollection: a collection of what? Stored where?

    – JB Nizet
    Mar 22 at 18:35











  • collection of 'std_ids' stored at that table I described.

    – Shvalb
    Mar 22 at 18:37












  • Each row of that table has one and only one std_id, stored in the column std_id of that same table. So there is no association at all, even less a collection. All you need is a column of type long.

    – JB Nizet
    Mar 22 at 18:39












  • there are multiple rows with uni_id=1 and std_id = 1,2,3 - please look at the example.

    – Shvalb
    Mar 22 at 18:40












  • So what? There are several persons with the name "John". That doesn't mean that a person, identified by a unique person_id, has several names. A person has just one name. Just like a uni_class identified by an id has just one std_id.

    – JB Nizet
    Mar 22 at 18:42













0












0








0








Looking for a solution to map multiple student_id rows under the same class_id, here is the table which has 3 fields:



integer id PRIMARY KEY
integer class_id;
integer student_id;


The table can have multiple rows under the same class_id like this:



Table = 'class'
id class_id student_id
1, 1, 1
2, 1, 2
3, 1, 3
4, 2, 1
5, 3, 1


I'm trying to Map this into a Pojo so I can use it as follow:



List<UniClass> uniClasses = session.createQuery("FROM ClassOfStudent", ClassOfStudent.class).getResultList();


I'm trying to solve this problem by using @ElementCollection



Here is my attempt:



@Entity
@Table(name = "class")
public class ClassOfStudents
@Id
@Column("id")
private long m_id;

@Column(name = "class_id")
private long m_classId;

@ElementCollection
@CollectionTable(name = "class", joinColumns = @JoinColumn(name="class_id"))
@Column(name = "student_id")
private List<Long> m_studentIds;

....



Any idea how to make this work?










share|improve this question
















Looking for a solution to map multiple student_id rows under the same class_id, here is the table which has 3 fields:



integer id PRIMARY KEY
integer class_id;
integer student_id;


The table can have multiple rows under the same class_id like this:



Table = 'class'
id class_id student_id
1, 1, 1
2, 1, 2
3, 1, 3
4, 2, 1
5, 3, 1


I'm trying to Map this into a Pojo so I can use it as follow:



List<UniClass> uniClasses = session.createQuery("FROM ClassOfStudent", ClassOfStudent.class).getResultList();


I'm trying to solve this problem by using @ElementCollection



Here is my attempt:



@Entity
@Table(name = "class")
public class ClassOfStudents
@Id
@Column("id")
private long m_id;

@Column(name = "class_id")
private long m_classId;

@ElementCollection
@CollectionTable(name = "class", joinColumns = @JoinColumn(name="class_id"))
@Column(name = "student_id")
private List<Long> m_studentIds;

....



Any idea how to make this work?







hibernate hibernate-mapping






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 13:59







Shvalb

















asked Mar 22 at 18:30









ShvalbShvalb

6121439




6121439












  • this table describes Many-to-Many relations: between what and what? I can't use @OneToMany since it's a single entity: A one-to-many between what and what? I'm trying to experiment with @ElementCollection: a collection of what? Stored where?

    – JB Nizet
    Mar 22 at 18:35











  • collection of 'std_ids' stored at that table I described.

    – Shvalb
    Mar 22 at 18:37












  • Each row of that table has one and only one std_id, stored in the column std_id of that same table. So there is no association at all, even less a collection. All you need is a column of type long.

    – JB Nizet
    Mar 22 at 18:39












  • there are multiple rows with uni_id=1 and std_id = 1,2,3 - please look at the example.

    – Shvalb
    Mar 22 at 18:40












  • So what? There are several persons with the name "John". That doesn't mean that a person, identified by a unique person_id, has several names. A person has just one name. Just like a uni_class identified by an id has just one std_id.

    – JB Nizet
    Mar 22 at 18:42

















  • this table describes Many-to-Many relations: between what and what? I can't use @OneToMany since it's a single entity: A one-to-many between what and what? I'm trying to experiment with @ElementCollection: a collection of what? Stored where?

    – JB Nizet
    Mar 22 at 18:35











  • collection of 'std_ids' stored at that table I described.

    – Shvalb
    Mar 22 at 18:37












  • Each row of that table has one and only one std_id, stored in the column std_id of that same table. So there is no association at all, even less a collection. All you need is a column of type long.

    – JB Nizet
    Mar 22 at 18:39












  • there are multiple rows with uni_id=1 and std_id = 1,2,3 - please look at the example.

    – Shvalb
    Mar 22 at 18:40












  • So what? There are several persons with the name "John". That doesn't mean that a person, identified by a unique person_id, has several names. A person has just one name. Just like a uni_class identified by an id has just one std_id.

    – JB Nizet
    Mar 22 at 18:42
















this table describes Many-to-Many relations: between what and what? I can't use @OneToMany since it's a single entity: A one-to-many between what and what? I'm trying to experiment with @ElementCollection: a collection of what? Stored where?

– JB Nizet
Mar 22 at 18:35





this table describes Many-to-Many relations: between what and what? I can't use @OneToMany since it's a single entity: A one-to-many between what and what? I'm trying to experiment with @ElementCollection: a collection of what? Stored where?

– JB Nizet
Mar 22 at 18:35













collection of 'std_ids' stored at that table I described.

– Shvalb
Mar 22 at 18:37






collection of 'std_ids' stored at that table I described.

– Shvalb
Mar 22 at 18:37














Each row of that table has one and only one std_id, stored in the column std_id of that same table. So there is no association at all, even less a collection. All you need is a column of type long.

– JB Nizet
Mar 22 at 18:39






Each row of that table has one and only one std_id, stored in the column std_id of that same table. So there is no association at all, even less a collection. All you need is a column of type long.

– JB Nizet
Mar 22 at 18:39














there are multiple rows with uni_id=1 and std_id = 1,2,3 - please look at the example.

– Shvalb
Mar 22 at 18:40






there are multiple rows with uni_id=1 and std_id = 1,2,3 - please look at the example.

– Shvalb
Mar 22 at 18:40














So what? There are several persons with the name "John". That doesn't mean that a person, identified by a unique person_id, has several names. A person has just one name. Just like a uni_class identified by an id has just one std_id.

– JB Nizet
Mar 22 at 18:42





So what? There are several persons with the name "John". That doesn't mean that a person, identified by a unique person_id, has several names. A person has just one name. Just like a uni_class identified by an id has just one std_id.

– JB Nizet
Mar 22 at 18:42












1 Answer
1






active

oldest

votes


















0














The trick that made this possible is that eventually I mapped the @Id of the class to be m_classId although it's not really the primary key of the table in the DB.



@Entity
@Table(name = "class")
public class ClassOfStudents implements Serializable

@Id
@Column(name = "class_id")
private long classId;

@ElementCollection(targetClass = Long.class, fetch = FetchType.EAGER)
@CollectionTable(name = "class", joinColumns = @JoinColumn(name = "class_id"))
@Column(name = "student_id", nullable = false)
private List<Long> m_studentIds;
...






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305830%2fhibernate-mapping-multi-rows-with-elementcollection-single-table-no-embeddabl%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The trick that made this possible is that eventually I mapped the @Id of the class to be m_classId although it's not really the primary key of the table in the DB.



    @Entity
    @Table(name = "class")
    public class ClassOfStudents implements Serializable

    @Id
    @Column(name = "class_id")
    private long classId;

    @ElementCollection(targetClass = Long.class, fetch = FetchType.EAGER)
    @CollectionTable(name = "class", joinColumns = @JoinColumn(name = "class_id"))
    @Column(name = "student_id", nullable = false)
    private List<Long> m_studentIds;
    ...






    share|improve this answer



























      0














      The trick that made this possible is that eventually I mapped the @Id of the class to be m_classId although it's not really the primary key of the table in the DB.



      @Entity
      @Table(name = "class")
      public class ClassOfStudents implements Serializable

      @Id
      @Column(name = "class_id")
      private long classId;

      @ElementCollection(targetClass = Long.class, fetch = FetchType.EAGER)
      @CollectionTable(name = "class", joinColumns = @JoinColumn(name = "class_id"))
      @Column(name = "student_id", nullable = false)
      private List<Long> m_studentIds;
      ...






      share|improve this answer

























        0












        0








        0







        The trick that made this possible is that eventually I mapped the @Id of the class to be m_classId although it's not really the primary key of the table in the DB.



        @Entity
        @Table(name = "class")
        public class ClassOfStudents implements Serializable

        @Id
        @Column(name = "class_id")
        private long classId;

        @ElementCollection(targetClass = Long.class, fetch = FetchType.EAGER)
        @CollectionTable(name = "class", joinColumns = @JoinColumn(name = "class_id"))
        @Column(name = "student_id", nullable = false)
        private List<Long> m_studentIds;
        ...






        share|improve this answer













        The trick that made this possible is that eventually I mapped the @Id of the class to be m_classId although it's not really the primary key of the table in the DB.



        @Entity
        @Table(name = "class")
        public class ClassOfStudents implements Serializable

        @Id
        @Column(name = "class_id")
        private long classId;

        @ElementCollection(targetClass = Long.class, fetch = FetchType.EAGER)
        @CollectionTable(name = "class", joinColumns = @JoinColumn(name = "class_id"))
        @Column(name = "student_id", nullable = false)
        private List<Long> m_studentIds;
        ...







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 25 at 20:04









        ShvalbShvalb

        6121439




        6121439





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305830%2fhibernate-mapping-multi-rows-with-elementcollection-single-table-no-embeddabl%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript