-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: ManyToMany with intermediate entity and GeneratedValue
PostPosted: Thu Jan 14, 2010 8:48 am 
Newbie

Joined: Fri Aug 22, 2008 6:12 am
Posts: 13
Hallo.

I have a problem with my ManyToMany mapping with an intermediate entity. My classes look like these:

Code:
@Entity
public class Inventory {

    private Long id;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "INVENTORY_ID")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    private List<InventoryUser> inventoryUser = new ArrayList<InventoryUser>();

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "inventory")
    public List<InventoryUser> getInventoryUser() {
        return inventoryUser;
    }

    public void setInventoryUser(List<InventoryUser> productUser) {
        this.inventoryUser = productUser;
    }
    // more attributes


Code:
@Entity
@Table(name = "User_Table")
public class User {
    private String id;

    @Id
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    private Set<InventoryUser> inventoryUser = new LinkedHashSet<InventoryUser>();

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "user")
    public Set<InventoryUser> getInventoryUser() {
        return inventoryUser;
    }

    public void setInventoryUser(Set<InventoryUser> inventoryUser) {
        this.inventoryUser = inventoryUser;
    }

    // more attributes


and the intermediate entity

Code:
@Entity
@Table(name = "Inventory_User")
@IdClass(InventoryUser.ProductUserPK.class)
public class InventoryUser {

    private Long inventoryId;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getInventoryId() {
        return inventoryId;
    }

    public void setInventoryId(Long inventoryId) {
        this.inventoryId = inventoryId;
    }

    private String userId;

    @Id
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    private User user;

    @ManyToOne
    @JoinColumn(name = "userId", insertable = false)
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    private Inventory inventory;

    @ManyToOne
    @JoinColumn(name = "INVENTORY_ID", insertable = false)
    public Inventory getInventory() {
        return inventory;
    }

    public void setInventory(Inventory inventory) {
        this.inventory = inventory;
    }

    private Date obtainDate = new Date();

    @Basic
    public Date getObtainDate() {
        return obtainDate;
    }

    public void setObtainDate(Date obtainDate) {
        this.obtainDate = obtainDate;
    }

    public static class ProductUserPK implements Serializable {
        private Long inventoryId;
        private String userId;

        public Long getInventoryId() {
            return inventoryId;
        }

        public void setInventoryId(Long inventoryId) {
            this.inventoryId = inventoryId;
        }

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }

        @Override
        public boolean equals(Object o) ...

        @Override
        public int hashCode() ...
    }
}


Now when i try to insert a new inventory - user relationship with following code:

Code:
        Inventory inventory = new Inventory();
        User user = new User();
        InventoryUser inventoryUser = new InventoryUser();
       
        inventoryUser.setInventory(inventory);
        inventoryUser.setUser(user);
     
        inventory.getInventoryUser().add(inventoryUser);
        user.getInventoryUser().add(inventoryUser);


I get an error:

Code:
Could not execute JDBC batch update [insert into Inventory_User (obtainDate, INVENTORY_ID, userId) values (?, ?, ?)]
java.sql.BatchUpdateException: Column 'INVENTORY_ID' cannot be null


It seems that InventoryUser does not get the generated vaule of Inventory!
What am I doing wrong? Please help!

_________________
believing in the impossible makes it possible


Top
 Profile  
 
 Post subject: Re: ManyToMany with intermediate entity and GeneratedValue
PostPosted: Thu Jan 14, 2010 10:20 am 
Regular
Regular

Joined: Thu Dec 10, 2009 10:53 am
Posts: 50
Here's a german language example how to solve this: (I've seen you also posted this question in the German forum)
http://www.jpa-hibernate.de/beispiele/beispiele.html
file: http://www.jpa-hibernate.de/beispiele/p ... klasse.zip

Your class Inventory_User is a so-called association class. Another way to implement this can be found at:
http://boris.kirzner.info/blog/archives ... osite-key/

The difference to the mechanism in the first example I can see, is that you have properties for the two IDs in both Inventory and User. I'm don't think that's a good idea. I'd also suggest using the @EmbeddedId annotation instead of IdClass as it works for me.


Top
 Profile  
 
 Post subject: Re: ManyToMany with intermediate entity and GeneratedValue
PostPosted: Mon Jan 18, 2010 6:45 am 
Newbie

Joined: Fri Aug 22, 2008 6:12 am
Posts: 13
Thank you!
The way with @EmbeddedId works perfectly :D

_________________
believing in the impossible makes it possible


Top
 Profile  
 
 Post subject: Re: ManyToMany with intermediate entity and GeneratedValue
PostPosted: Mon Jan 18, 2010 7:45 am 
Newbie

Joined: Fri Aug 22, 2008 6:12 am
Posts: 13
Ok, i have to put this threat up again, sorry.

I have changed everything as described @ http://boris.kirzner.info/blog/archives/2008/07/19/hibernate-annotations-the-many-to-many-association-with-composite-key/ and persisting works fine, but when I serach (with FatchMode = EAGER) I always get a StackOverflow - even there is only 1 entry in DB

Any ideas what this is about?

_________________
believing in the impossible makes it possible


Top
 Profile  
 
 Post subject: Re: ManyToMany with intermediate entity and GeneratedValue
PostPosted: Tue Jan 19, 2010 4:43 am 
Regular
Regular

Joined: Thu Dec 10, 2009 10:53 am
Posts: 50
Quote:
Any ideas what this is about?


Not really, I haven't seen that error. Can you query objects with HQL or Criteria? What does your code look like now? What's the exact error message?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.