-->
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.  [ 2 posts ] 
Author Message
 Post subject: Unidirectional OneToMany relationship not inserting
PostPosted: Wed Jan 03, 2007 6:40 pm 
Newbie

Joined: Wed Jan 03, 2007 5:59 pm
Posts: 2
Location: Michigan
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.2

Name and version of the database you are using: MySQL 5.0x

I'm having a problem with inserting a unidirectional OneToMany relation when the both objects already exists in the Hibernate context, but do not have an existing relationship.

Some background
  • The customer table is populated by an external source
  • The customer objects are preloaded into the context before a mapping is attempted
  • The customer group objects are created by the application
  • If the customer group object is new the insert works correctly.


Database SQL
Code:
Create table customer (
   customer_id Varchar(100) NOT NULL,
   UNIQUE (customer_id),
Primary Key (customer_id)) ENGINE = InnoDB;

Create table customer_group (
   customer_group_id Int UNSIGNED NOT NULL AUTO_INCREMENT,
   name Varchar(250),
Primary Key (customer_group_id)) ENGINE = InnoDB;

Create table customer_group_customer (
   customer_group_id Int UNSIGNED NOT NULL,
   customer_id Varchar(100) NOT NULL,
Primary Key (customer_group_id,customer_id)) ENGINE = InnoDB;

Alter table customer_group_customer add Foreign Key (customer_id) references customer (customer_id) on delete  restrict on update  restrict;
Alter table customer_group_customer add Foreign Key (customer_group_id) references customer_group (customer_group_id) on delete  restrict on update  restrict;


Customer Group Object
Code:
@Entity
@Table(name = "customer_group")
public class CustomerGroup implements Serializable
{
  private long id = -1;

  private String name;

  private List<Customer> customers = new ArrayList<Customer>();

  ...

  @Id
  @GeneratedValue
  @Column(name = "customer_group_id")
  public long getId()
  {
    return id;
  }

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

  @Basic
  @Column(name = "name", length = 255, nullable = false)
  public String getName()
  {
    return name;
  }

  public void setName(String name)
  ...

  @OneToMany(cascade = {})
  @JoinTable(name = "customer_group_customer",
             joinColumns = {@JoinColumn(name = "customer_group_id")},
             inverseJoinColumns = {@JoinColumn(name = "customer_id")})
  public List<Customer> getCustomers()
  {
    return customers;
  }

  public void setCustomers(List<Customer> customers)
  ...
}


Customer Object
Code:
@Entity
@Table(name = "customer")
public class CustomerMapping implements Serializable
{
  private String customerId;

   ...
   
  @Id
  @Column(name = "customer_id")
  public String getCustomerId()
  {
    return customerId;
  }

  public void setCustomerId(String customerId)
  ...
}


Test Code
Code:
...
HibernateEntityManager em = (HibernateEntityManager)factory.createEntityManager();
em.createQuery("from Customer").getResultList();

// I know that there is a group with ID 1
CustomerGroup group = getEntityManager().find("CustomerGroup", (Long)1);


Customer customer = new Customer("CID0017X7");

group.getCustomers().add(customer);
System.out.println("SAVING");

EntityTransaction trans = em.getTransaction();
trans.begin();

em.merge(group);

trans.commit();


Standard Output
Code:
Hibernate: select customerma0_.customer_id as customer1_1_ from customer customerma0_
Hibernate: select customergr0_.customer_group_id as customer1_2_0_, customergr0_.name as name2_0_ from customer_group customergr0_ where customergr0_.customer_group_id=?
Hibernate: select customerma0_.customer_group_id as customer1_1_, customerma0_.customer_id as customer2_1_, customerma1_.customer_id as customer1_1_0_ from customer_group_customer customerma0_ left outer join customer customerma1_ on customerma0_.customer_id=customerma1_.customer_id where customerma0_.customer_group_id=?
SAVING
Hibernate: select customerma0_.customer_id as customer1_1_0_ from customer customerma0_ where customerma0_.customer_id=?


As you can see from the output it does a few selects but the relation is never inserted into the database. Is there something that I am missing, or is this a limitation of the JPA or Hibernate?

Thanks


Top
 Profile  
 
 Post subject: Unidirectional OneToMany relationship not inserting
PostPosted: Thu Jan 04, 2007 2:13 pm 
Newbie

Joined: Wed Jan 03, 2007 5:59 pm
Posts: 2
Location: Michigan
I have figured out that merge the detached object first then making modifications work fine, but I still have a problem when merging a detached object, which already has had changes. The changes are represented in the Hibernate context, but not persisted to the database. Any ideas?

Test Code
Code:
...
HibernateEntityManager em = (HibernateEntityManager)factory.createEntityManager();
em.createQuery("from Customer").getResultList();

// I know that there is a group with ID 1
CustomerGroup group = getEntityManager().find("CustomerGroup", (Long)1);
System.out.println("group.getCustomers(): " + group.getCustomers());

Customer customer = new Customer("CID0017X7");

// Create Detached object
CustomerGroup newGroup = new CustomerGroup(group.getId(), group.getName(), group.getCustomers());

newGroup.getCustomers().add(customer);

System.out.println("MERGING");

EntityTransaction trans = em.getTransaction();
trans.begin();

CustomerGroup group1 = em.merge(newGroup);

trans.commit();

System.out.println("group1.getCustomers(): " + group1.getCustomers());



Standard Output
Code:
Hibernate: select customerma0_.customer_id as customer1_1_ from customer customerma0_
Hibernate: select customergr0_.customer_group_id as customer1_2_0_, customergr0_.name as name2_0_ from customer_group customergr0_ where customergr0_.customer_group_id=?
Hibernate: select customerma0_.customer_group_id as customer1_1_, customerma0_.customer_id as customer2_1_, customerma1_.customer_id as customer1_1_0_ from customer_group_customer customerma0_ left outer join customer customerma1_ on customerma0_.customer_id=customerma1_.customer_id where customerma0_.customer_group_id=?
group.getCustomerMappings(): [CID001X6, CID0002X3, CIDD803X0]
MERGING
Hibernate: select customerma0_.customer_id as customer1_1_0_ from customer customerma0_ where customerma0_.customer_id=?
group1.getCustomerMappings(): [CID001X6, CID0002X3, CIDD803X0, CID0017X7]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.