Results 1 to 16 of 16
  1. #1
    Max0314 is offline Member
    Join Date
    Sep 2015
    Posts
    15
    Rep Power
    0

    Question Why in table makes NULL, when using mappedBy?

    Hi, everyone.
    I've got problem.
    Trying save in DB a little Library, that have some books. Take a look on the code:

    Library entity.
    Java Code:
    package main.java.table;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.persistence.CascadeType;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    
    @Entity
    @Table(name = "library")
    public class Library {
    	@Id
    	@GeneratedValue(strategy = GenerationType.IDENTITY)
    	long id;
    	String lib_name;
    	@OneToMany(cascade = CascadeType.ALL, mappedBy="library", orphanRemoval = true)
    	List<BookItem> books = new ArrayList<BookItem>();
     //  getters...setters doesn't show brevity sake
    BookItem entity.
    Java Code:
    package main.java.table;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;
    
    @Entity
    @Table(name = "books")
    public class BookItem {
    	@Id
    	@GeneratedValue(strategy = GenerationType.IDENTITY)
    	long id;
    	String name;
    	@ManyToOne
    	//@JoinColumn(name = "lib_id")
    	Library library;
    Next, i save it o DBMS by piece of code.
    Java Code:
    		Library lib=new Library();
    		List<BookItem> books=lib.getBooks();
    		lib.setLib_name("Brook's library");
    		BookItem bk1=new BookItem("First Book");
    		BookItem bk2=new BookItem("Second Book");
    		BookItem bk3=new BookItem("Thrid Book");
    		books.add(bk1);
    		books.add(bk2);
    		books.add(bk3);
    		
    		session.save(lib);
    		
    		Library lib2=new Library();
    		List<BookItem> books2=lib2.getBooks();
    		lib2.setLib_name("Brook's library #2");
    		BookItem bk21=new BookItem("#First Book");
    		BookItem bk22=new BookItem("#Second Book");
    		BookItem bk23=new BookItem("#Thrid Book");
    		books2.add(bk21);
    		books2.add(bk22);
    		books2.add(bk23);
    		
    		session.save(lib2);
          // getters-setters - Ditto
    But, in result i have empty foreign key. Why???
    mysql> select * from books;
    +----+--------------+------------+
    | id | name | library_id |
    +----+--------------+------------+
    | 1 | First Book | NULL |
    | 2 | Second Book | NULL |
    | 3 | Thrid Book | NULL |
    | 4 | #First Book | NULL |
    | 5 | #Second Book | NULL |
    | 6 | #Thrid Book | NULL |
    +----+--------------+------------+
    6 rows in set (0.00 sec)
    If omit mappedBy, Hibernate just make link table library_books, BUT in the table books still exust library_id, that still NULL. Why?
    Last edited by Max0314; 10-23-2015 at 12:09 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Why in table makes NULL, when using mappedBy?

    Why is the @JoinColumn on BookItem.library commented out?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Max0314 is offline Member
    Join Date
    Sep 2015
    Posts
    15
    Rep Power
    0

    Default Re: Why in table makes NULL, when using mappedBy?

    Just because it is not mandatory, its otional.
    By the way, if uncomment that annotation, nothing to change, except name of column in the table "books". I'll try it many times.

    I think foundation of problem into mappedBy attribute. Thats is responsibility of creation and using foreign key.

    Ok, let make a little changes. Explicitly depend link table and name it.

    Java Code:
    @Entity
    @Table(name = "library")
    public class Library {
    	@Id
    	@GeneratedValue(strategy = GenerationType.IDENTITY)
    	long id;
    	String lib_name;
    	@OneToMany(cascade = CascadeType.ALL)
    	@JoinTable(name = "l_b", joinColumns = {
    			@JoinColumn(name = "library_id") }, inverseJoinColumns = @JoinColumn(name = "book_id") )
    	List<BookItem> books = new ArrayList<BookItem>();
    BookItem doesn't change.



    So, appears a table named "l_b", and we have now:
    Java Code:
    mysql> select * from books;
    +----+--------------+------------+
    | id | name         | library_id |
    +----+--------------+------------+
    |  1 | First Book   |       NULL |
    |  2 | Second Book  |       NULL |
    |  3 | Thrid Book   |       NULL |
    |  4 | #First Book  |       NULL |
    |  5 | #Second Book |       NULL |
    |  6 | #Thrid Book  |       NULL |
    +----+--------------+------------+
    6 rows in set (0.00 sec)
    
    mysql> select * from l_b;
    +------------+---------+
    | library_id | book_id |
    +------------+---------+
    |          1 |       1 |
    |          1 |       2 |
    |          1 |       3 |
    |          2 |       4 |
    |          2 |       5 |
    |          2 |       6 |
    +------------+---------+
    6 rows in set (0.00 sec)
    
    mysql> select * from library;
    +----+--------------------+
    | id | lib_name           |
    +----+--------------------+
    |  1 | Brook's library    |
    |  2 | Brook's library #2 |
    +----+--------------------+
    2 rows in set (0.00 sec)
    Why in the "books" table present library_id column? And why it consist NULL?

    Oh, I toitally forget to show hibernate.cfg.xml:
    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
      
    <hibernate-configuration>  
        <session-factory>  
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
            <property name="hibernate.connection.username">root</property>  
            <property name="hibernate.connection.password"></property>  
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bookstore</property>  
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="hbm2ddl.auto">create</property>  
            <property name="show_sql">true</property>           
            <property name="connection.pool_size">1</property>
              
            // That classes from others examples  
            <mapping class="main.java.table.Book"></mapping>
            <mapping class="main.java.table.Person"></mapping>
            <mapping class="main.java.table.Observer"></mapping>
            <mapping class="main.java.table.Skill"></mapping>
            <mapping class="main.java.table.Ranking"></mapping>
            <mapping class="main.java.table.People"></mapping>
            <mapping class="main.java.table.Customer"></mapping>
           // That is for current example
            <mapping class="main.java.table.Library"></mapping>
            <mapping class="main.java.table.BookItem"></mapping>       
      
        </session-factory>  
    </hibernate-configuration>
    Last edited by Max0314; 10-23-2015 at 12:31 PM.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Why in table makes NULL, when using mappedBy?

    It doesn't really help to use JPA annotations and then force in Hibernate configuration files anyway. A mix/match might originate from that. I would get rid of the hibernate config file altogether and stick with a proper JPA persistence.xml.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Max0314 is offline Member
    Join Date
    Sep 2015
    Posts
    15
    Rep Power
    0

    Default Re: Why in table makes NULL, when using mappedBy?

    Oh, unfortunately, my skill is not too high. I just start to learn Hibernate.

    I wonder, how you will solve this problem, if it arise on your work, and you forced to use Hibernate only?

    Ok, thanks anyway. (:
    Last edited by Max0314; 10-23-2015 at 12:46 PM.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Why in table makes NULL, when using mappedBy?

    Quote Originally Posted by Max0314 View Post
    I wonder, how you will solve this problem, if it arise on your work, and you forced to use Hibernate only?
    You are not in that situation. You are using Hibernate and you are using JPA as the API. now you need to go learn what that actually means. I suggest the book "Pro JPA 2".

    Pro JPA 2 (Expert&#39;s Voice in Java): Mike Keith, Merrick Schincariol: 9781430249269: Amazon.com: Books

    and because nobody wants to actually invest in their own success, here is a free resource:

    https://docs.oracle.com/javaee/7/tut...sist.htm#BNBPY
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Max0314 is offline Member
    Join Date
    Sep 2015
    Posts
    15
    Rep Power
    0

    Default Re: Why in table makes NULL, when using mappedBy?

    I'm sure, you are right. But another problem is I'm not properly own English. And reading new book become to long quest. Especially, when start read the new book, doesn't finish prior one.

    I am thank you, for your hints, anyway. :)

  8. #8
    Max0314 is offline Member
    Join Date
    Sep 2015
    Posts
    15
    Rep Power
    0

    Default Re: Why in table makes NULL, when using mappedBy?

    Wait a second. Am I correct to understand you? Do you propose to use JPA directly? To use "EntityManager" instead "Session" and etc? But what the point? Eventually, what is the differences?
    Last edited by Max0314; 10-26-2015 at 09:26 AM.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Why in table makes NULL, when using mappedBy?

    You are inventing things. You can identify when that happens when you start to claim someone else is saying things while you are the first person to use particular phrasing and wording. I only spoke about configuration files, you are the one that has made the jump to code.

    Good luck.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    Max0314 is offline Member
    Join Date
    Sep 2015
    Posts
    15
    Rep Power
    0

    Default Re: Why in table makes NULL, when using mappedBy?

    Let's start over.

    I've got some weird in work with Hibernate. You propose... What is you hropose to do? Use JPA percictence, i.e. rid hibernate.cfg.xml, make percistence.xml, then in code acquare EntityManagr, then Session, and do it's all I did previously, but with that instance of Session?

    What is will be changed?

    Please, say simply, as you can.

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Why in table makes NULL, when using mappedBy?

    I'm not going to repeat myself. If you don't understand what I'm saying then you need to study the things I've shared with you already, I can't make you understand what you refuse to go ahead and understand. Sorry, a forum is not a replacement for a private tutor. I can't make you understand, you need to take ownership of it yourself and go out there and figure it out.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    Max0314 is offline Member
    Join Date
    Sep 2015
    Posts
    15
    Rep Power
    0

    Default Re: Why in table makes NULL, when using mappedBy?

    It sounds so sad to me, man.

    But you did not answer a simple question - what benefits by using JPA-annotations directly, if still needed get EntityManager, then get Session and work with Hibernate, anyway. What is changed?

    I think, I'll make you mad. (:

  13. #13
    Max0314 is offline Member
    Join Date
    Sep 2015
    Posts
    15
    Rep Power
    0

    Default Re: Why in table makes NULL, when using mappedBy?

    Dude, are you alive there?

  14. #14
    Max0314 is offline Member
    Join Date
    Sep 2015
    Posts
    15
    Rep Power
    0

    Default Re: Why in table makes NULL, when using mappedBy?

    Is anybody can help me?

  15. #15
    Max0314 is offline Member
    Join Date
    Sep 2015
    Posts
    15
    Rep Power
    0

    Default Re: Why in table makes NULL, when using mappedBy?

    Dude, probably I solve the problem. It simple.

    Do you want to know how? ( I think you do not, but just asking :) )
    Last edited by Max0314; 11-10-2015 at 07:43 PM.

  16. #16
    Max0314 is offline Member
    Join Date
    Sep 2015
    Posts
    15
    Rep Power
    0

    Default Re: Why in table makes NULL, when using mappedBy?

    Well. Seems dude got little offense.

    Ok. The solve is:

    Java Code:
    Library lib=new Library();
      List<BookItem> books=lib.getBooks();
      lib.setLib_name("Brook's library");
      BookItem bk1=new BookItem("First Book");
      BookItem bk2=new BookItem("Second Book");
      BookItem bk3=new BookItem("Thrid Book");
    
      books.add(bk1);
      books.add(bk2);
      books.add(bk3);
    
       lib.setBooks(books);      // solve
       
      session.save(lib);
       
      Library lib2=new Library();
      List<BookItem> books2=lib2.getBooks();
      lib2.setLib_name("Brook's library #2");
      BookItem bk21=new BookItem("#First Book");
      BookItem bk22=new BookItem("#Second Book");
      BookItem bk23=new BookItem("#Thrid Book");
      books2.add(bk21);
      books2.add(bk22);
      books2.add(bk23);
       lib2.setBooks(books2);      // solve
       
      session.save(lib2);

Similar Threads

  1. mappedBy and joinColumn
    By jmohandos304 in forum Hibernate
    Replies: 2
    Last Post: 11-06-2014, 01:08 PM
  2. Replies: 1
    Last Post: 06-19-2013, 08:18 PM
  3. Replies: 13
    Last Post: 12-08-2011, 02:21 AM
  4. Replies: 6
    Last Post: 10-18-2010, 10:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •