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 ObjectCode:
@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 ObjectCode:
@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 CodeCode:
...
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 OutputCode:
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