-->
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: Issue with @EmbeddableSuperclass and a Bi-Directional OTM
PostPosted: Tue Sep 20, 2005 4:23 pm 
Newbie

Joined: Fri Aug 13, 2004 12:17 pm
Posts: 12
Location: St. Louis, MO
Hibernate version:3.1b5 + 3.1b3 annotations

I'm encountering a problem with using a base entity (assigned using @EmbeddableSuperclass) in a @ManyToOne relationship. Long story short: When attempting to save, it is failing due to a null entry being saved to the fk column. It seems it is failing to pick up the id of the FK class (which uses the base entity)....when I remove the base entity from the hierarchy and place the id methods in the fk class itself, it finds the id and saves the otm/mto just fine. Again, this works when not using an @EmbbedableSuperclass

Usage
Code:
...
Company c = new Company();
c.setId(1);//there is a company record with this id in the database

Address a = new Address();
a.setCity("Norman");
a.setState("OK");
a.setStreet1("123 Main St.");     
a.setZip("73072");
a.setCompany(c);

dao.save(a);

...


Note param 3
Code:
...
15:04:51.484 DEBUG (org.hibernate.jdbc.AbstractBatcher:324) - insert into Address (OPT_LOCK, city, company_fk, street1, street2, zip, state, id) values (?, ?, ?, ?, ?, ?, ?, null)
Hibernate: insert into Address (OPT_LOCK, city, company_fk, street1, street2, zip, state, id) values (?, ?, ?, ?, ?, ?, ?, null)
15:04:51.484 DEBUG (org.hibernate.jdbc.AbstractBatcher:377) - preparing statement
15:04:51.484 DEBUG (org.hibernate.persister.entity.BasicEntityPersister:1671) - Dehydrating entity: [com.sonicdrivein.portfolio.domain.model.Address#<null>]
15:04:51.484 DEBUG (org.hibernate.type.NullableType:60) - binding '0' to parameter: 1
15:04:51.484 DEBUG (org.hibernate.type.NullableType:60) - binding 'norman' to parameter: 2
15:04:51.484 DEBUG (org.hibernate.type.NullableType:53) - binding null to parameter: 3
15:04:51.484 DEBUG (org.hibernate.type.NullableType:60) - binding '207 harvard' to parameter: 4
15:04:51.500 DEBUG (org.hibernate.type.NullableType:53) - binding null to parameter: 5
15:04:51.500 DEBUG (org.hibernate.type.NullableType:60) - binding '73072' to parameter: 6
15:04:51.500 DEBUG (org.hibernate.type.NullableType:60) - binding 'OK' to parameter: 7
...


Company (One side of the MTO/OTM)
Code:
@Entity
public class Company extends AbstractEntity{
// ------------------------------ FIELDS ------------------------------

   private Set<Address> addresses;
   private String name;
   private String symbol;

// --------------------------- CONSTRUCTORS ---------------------------

   public Company() {
      super();
   }

   public Company(String name, String symbol) {
      this();
      this.name = name;
      this.symbol = symbol;
   }

// --------------------- GETTER / SETTER METHODS ---------------------

   @OneToMany(mappedBy = "company")
   public Set<Address> getAddresses() {
      return addresses;
   }

   public String getName() {
      return name;
   }

   @Column(unique = true)
   public String getSymbol() {
      return symbol;
   }
...
}

Address (Many side of the MTO/OTM)
Code:
@Entity
public class Address extends AbstractEntity {
// ------------------------------ FIELDS ------------------------------

   private Company company;
   private String city;
   private String state;
   private String street1;
   private String street2;

   private String zip;

// --------------------------- CONSTRUCTORS ---------------------------

   public Address() {
   }

   public Address(String zip, String street1, String state, String city) {
      this.zip = zip;
      this.street1 = street1;
      this.state = state;
      this.city = city;
   }

// --------------------- GETTER / SETTER METHODS ---------------------

   public String getCity() {
      return city;
   }

   @ManyToOne
   @JoinColumn(name="company_fk")
   public Company getCompany() {
      return company;
   }
...
}


AbstractEntity
Code:
@EmbeddableSuperclass
public abstract class AbstractEntity implements Serializable {
// ------------------------------ FIELDS ------------------------------

   private Integer id;
   private Integer version;

// --------------------- GETTER / SETTER METHODS ---------------------

   @Id(generate = GeneratorType.IDENTITY)
   public Integer getId(){
      return id;
   }

   @Version
   @Column(name = "OPT_LOCK")
   public Integer getVersion() {
      return version;
   }

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

   public void setVersion(Integer version) {
      this.version = version;
   }
}

DDL
Code:
create table Address (
    id integer generated by default as identity (start with 1),
    OPT_LOCK integer,
    city varchar(255),
    street1 varchar(255),
    street2 varchar(255),
    zip varchar(255),
    state varchar(255),
    company_fk integer,
    primary key (id)
);
create table Company (
    id integer generated by default as identity (start with 1),
    OPT_LOCK integer,
    name varchar(255),
    symbol varchar(255),
    primary key (id),
    unique (symbol)
);
alter table Address
    add constraint FK1ED033D42684B579
    foreign key (company_fk)
    references Company;


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 5:40 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Put the runnable testcase in JIRA. Thanks

_________________
Emmanuel


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.