-->
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.  [ 25 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: I got problems with the bidirectional relationships
PostPosted: Mon May 15, 2006 5:05 am 
Newbie

Joined: Fri Jan 20, 2006 2:19 am
Posts: 4
Hi,

I think I've managed to get the unidirectional stuff working, but when I add the bidirectional relationships i end up in an endless loop (the same sql query is executed untill stack overflows).

Could somebody please add the annotations in all of the classes in one post so that I can track where my stuff goes wrong..

Thanks!
/M


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 16, 2006 6:47 am 
Beginner
Beginner

Joined: Fri Apr 28, 2006 3:04 am
Posts: 22
Location: Amsterdam
ALL the annotations in the mappings are in my example.. It should work using it exactly this way..


http://java-aap.blogspot.com/2006/04/hi ... html#links




here is my testCase:

Code:
   /**
    * - creates a product
    * - creates an item
    * - creates a productItem with references to product & item and insert
    *
    * result after refresh:
    * - product's list contains a productItem
    * - item's list contains a productItem
    */
   public void testInsertGet()
   {
      Product product = getStoredProduct();
      Item item = getStoredItem();
      
      ProductItem productItem = new ProductItem();
      productItem.setItem(item);
      productItem.setProduct(product);
      productItem.setDescription("test");
      productItemDao.insert(productItem);
      
      productDao.refresh(product);
      itemDao.refresh(item);
      
      assertTrue(product.getProductItems().contains(productItem));
      assertTrue(item.getProductItems().contains(productItem));
   }


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 10:34 am 
Beginner
Beginner

Joined: Fri Apr 28, 2006 3:04 am
Posts: 22
Location: Amsterdam
Hmm Found another error in my annotationsolutions;

The Item mapping to fake hibernate in your composite table should be of type Long .. otherwise you will get an error when selecting it from db (Long to BINARY error).

updated in my blog also

_________________
Greetz, Marcel - http://java-aap.blogspot.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 25, 2006 2:40 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Can someone sum up that in a runnable JIRA testcase so that I can keep track of it and fix whatever need to be fixed.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 25, 2006 2:50 pm 
Beginner
Beginner

Joined: Wed Apr 26, 2006 2:41 pm
Posts: 30
It seems that regardless of the viability of the technique in the blog posting it exposes a few bugs in Hibernate that that people have brought up with this kind of mapping:

* PK Class does not work if it is an inner class
* Annotations only work here if they are on the fields, not the methods
* It only works with this one style of mapping the pk fields? Not sure but it seems like we should be able to do it with @EmbeddedId or @IdClass annotations
* You have to fake Hibernate out with extra fields on your entity bean to get it to work.
* Does it work with the annotations specified as overrides in orm.xml? I havent tried this yet.

The Oreilly Enterprise Java Beans 3.0 book I preordered was waiting for me last night in the mail and I was hoping it would address this mapping type explicitly but at least on first glance, it doesn't appear to. (Looks like a pretty good book though, btw) I'm going to give it a more thorough reading next week.

I think adding a section to the Hibernate documentation on this giving a cannonical example would be helpful. Also I think it would be useful to open Jira issues for each of the above with test code so we can get definitive answers on these issues. I'm going to look into how hard it is to create an submit a Jira test case next week if I can get some free time.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 3:48 pm 
Newbie

Joined: Mon Mar 20, 2006 8:07 am
Posts: 4
Is a testcase still required? If so I will create and submit one tomorrow. I have this working using .hbm.xml mapping; I assume that using annotations it should not be necessary to alter the java class structure at all i.e. for the class containing a composite foreign key

Code:
public class AB {
   private static class Id implements Serializable {
      public int idA;
      public int idB;

      public ID() {}

      public ID(int idA, int idB) {
         this.idA = idA;
         this.idB = idB;
      }

      public int hashcode() {
        ...
      }

      public boolean equals(Obj other) {
       ...
   }

   private Id id = new Id();
   private A a;
   private B b;

   ...

   public AB() {}
   
   public AB (A a, B b) {
      this.a = a;
      this.id.idA = a.getId();
      this.b = b;
      this.id.idB = b.getId();
   }

   public void setA(A a) {
      this.a = a;
   }

   public void setB(B b) {
      this.b = b;
   }

   public A getA() {
      return a;
   }

   public B getB() {
      return b;
   }

   ...
}

where A and B are the other classes involved in the manytomany relationship (which has attributes on the join) rather than the discussinverseed above of having A and B in the EmbeddedID class and their Ids in the main class (as it is IMO neater that way).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 2:25 pm 
Newbie

Joined: Mon Mar 20, 2006 8:07 am
Posts: 4
http://opensource.atlassian.com/project ... se/ANN-360 includes testcase

I've included my hibernate mapping version here in the hope that someone can tell me the mapping to use for annotations and that the bug isn't actually a bug:

Code:
<hibernate-mapping package="test.manytomanywithattributes" default-access="field">
   
   
   <class name="ChargeType">
      <id name="chargeTypeId">
         <generator class="native" />
      </id>
      <property name="description" />
      <bag name="charges" inverse="true" fetch="join" lazy="true">
         <key>
            <column name="chargeTypeId"/>
         </key>
         <one-to-many class="Charge"/>
      </bag>
   </class>
   
   <class name="Charge">
      <composite-id name="id" class="ChargePk">
         <key-property name="chargeTypeId" />
         <key-property name="productId" />
      </composite-id>
      <property name="deposit" />
      <many-to-one name="chargeType" insert="false" update="false" not-null="true">
         <column name="chargeTypeId" />
      </many-to-one>
      <many-to-one name="product" insert="false" update="false" not-null="true">
         <column name="productId" />
      </many-to-one>
   </class>
   
   
   <class name="Product">
      <id name="productId">
         <generator class="native" />
      </id>
      <property name="description" />
      <bag name="charges" inverse="true" lazy="true">
         <key>
            <column name="productId"/>
         </key>
         <one-to-many class="Charge"/>
      </bag>
   </class>
</hibernate-mapping>


TIA

Peter
--
I will be away until 12th June; I shall be able to comment further here/on the bug then


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 21, 2007 11:11 am 
Newbie

Joined: Fri Sep 21, 2007 10:51 am
Posts: 1
Hi all, I guess I have the same problem of people who post here...
I'm using hibernate 3.2 and hibernate annotation 3.3GA.ù

My database confuguration is:
Code:

CREATE TABLE  `mercatino`.`customers` (
  `id` int(10) NOT NULL auto_increment,
  `type` tinyint(2) default NULL,
  `firstname` varchar(20) default NULL,
  `lastname` varchar(30) default NULL,
  `address` varchar(30) default NULL,
  `zip_code` varchar(5) default NULL,
  `city` varchar(30) default NULL,
  `region` varchar(2) default NULL,
  `fiscal_code` varchar(16) default NULL,
  `piva` varchar(16) default NULL,
  `id_number` varchar(7) default NULL,
  `destination_doc` varchar(30) default NULL,
  `phone1` varchar(10) default NULL,
  `phone2` varchar(10) default NULL,
  `mobile` varchar(11) default NULL,
  `fax` varchar(11) default NULL,
  `email` varchar(35) default NULL,
  `payment_notes` varchar(50) default NULL,
  `prev_datas` int(10) default NULL,
  `delete` int(2) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE  `mercatino`.`movements` (
  `id_product` int(10) NOT NULL,
  `id_customer` int(10) NOT NULL,
  `type` varchar(10) NOT NULL,
  `date` varchar(10) NOT NULL,
  `during` int(3) NOT NULL,
  `end_description` varchar(30) NOT NULL,
  `price` int(10) NOT NULL,
  PRIMARY KEY  (`id_product`,`id_customer`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE  `mercatino`.`products` (
  `id` int(10) NOT NULL auto_increment,
  `description` varchar(50) default NULL,
  `notes` varchar(50) default NULL,
  `lotto` int(10) default NULL,
  `sold` int(1) default '0' COMMENT '0 = not sold, 1 = sold',
  `code` varchar(15) default NULL COMMENT 'data + progressive number (maybe id number)',
  `provvigion` int(10) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;



So, my problem is the bind of composite primary key in movements table to other 2 tables.

here there are my class:

Code:

@Entity
@Table (name ="movements")
/*@AssociationOverrides({
@AssociationOverride(name="id.product", joinColumns = @JoinColumn(name="id_product")),
@AssociationOverride(name="id.customer", joinColumns = @JoinColumn(name="id_customer"))
})*/
//@IdClass(MovementPrimaryKey.class)
public class Movements implements Serializable{
   
    @Id
    private MovementPrimaryKey id = new MovementPrimaryKey();
   
   
    @SuppressWarnings("unused")
    @Column(name="id_product", nullable=false, updatable=false, insertable=false)
    private Long id_product;

    @SuppressWarnings("unused")
    @Column(name="id_customer", nullable=false, updatable=false, insertable=false)
    private Long id_customer;
   
   
    private String type;
    private String date;
    private int during;
    private String end_description;
    private int price;
   
    public Movements() {    }
   
    //some getters and setters
   
    public Customer getId_customer() {
        return id.getId_customer();
    }

    public void setId_customer(Customer id_customer) {
        id.setId_customer(id_customer);
    }
   
   
    public Product getId_product() {
        return id.getId_product();
    }

    public void setId_product(Product id_product) {
        id.setId_product(id_product);
    }

}


@Entity
@Table(name="products")
public class Product implements Serializable {
   
    public Product(){ }
   
   
    private Long id;
    //private String name;
    private String description;
    //private int buy_price;
    //private int sell_price;
    private long provvigion;
    private String notes;
    /*private String cod_end_contract;
    private String description_end_contract;
    private String start_data;
    private int during;*/
    //private String product_type;
    private String code;
    private int sold;
    private Lotto lotto;
   
    /*private Set customers = new HashSet();
    private Set provvigions = new HashSet();
    private Set lottos = new HashSet();
    private Set productTypes = new HashSet();*/
   
    private List<Movements> movements = new ArrayList();


    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    @ManyToOne
    public Long getId() {
        return id;
    }

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

    //some getters and setters

    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, targetEntity=net.bayzone.mercatino.tables.Lotto.class )
    @JoinColumn(name="lotto")
    public Lotto getLotto() {
        return lotto;
    }

    public void setLotto(Lotto lotto) {
        this.lotto = lotto;
    }

    public boolean equals(Object other) {
        if (this == other) return true;
        if ( !(other instanceof Product) ) return false;
            final Product cat = (Product) other;
        if ( !cat.getId().equals( getId() ) ) return false;
        return true;
    }
    public int hashCode() {
        int result;
        result = getId().hashCode();
        result = 29 * result + Integer.parseInt(getId().toString());
        return result;
    }

    @OneToMany(mappedBy="id.id_product")
    public List<Movements> getMovements() {
        return movements;
    }

    public void setMovements(List movements) {
        this.movements = movements;
    }

}


@Entity
@Table(name="customers")
public class Customer implements Serializable{
   
    public Customer() { }
   
    private Long id;
    private int type;
    private String firstname;
    private String lastname;
    private String address;
    private String zip_code;
    private String city;
    private String region;
    private String fiscal_code;
    private String piva;
    private String id_number;
    private String destination_doc;
    private String phone1;
    private String phone2;
    private String mobile;
    private String fax;
    private String email;
    private String payment_notes;
    private Long prev_datas;
   
    private List<Movements> movements= new ArrayList();
   
    //private Set products = new HashSet();
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    @ManyToOne
    public Long getId() {
        return id;
    }

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

    //some getters and setters

    @OneToMany(mappedBy="id.id_customer")
    public List<Movements> getMovements() {
        return movements;
    }

    public void setMovements(List movements) {
        this.movements = movements;
    }
}




There is also another table called lottos but is not important here.If you think is important to resolve my problem, tell me and I will post here.

so, all the file work, but when I try to insert data, I receive this error:

Code:
Hibernate: insert into movements (date, during, end_description, price, type, id_customer, id_product) values (?, ?, ?, ?, ?, ?, ?)
Sep 21, 2007 4:51:28 PM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1366, SQLState: HY000
Sep 21, 2007 4:51:28 PM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Incorrect integer value: '’' for column 'id_customer' at row 1


Someone can help me? I'm becaming fool...

Thanks a lot in advance..Tell me if you need some information...

bye
Luca

_________________
[ http://whitenoise.netsons.org ]
[ http://bayzone.wordpress.com ]
[ http://www.clandellabirra.net ]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 25, 2007 3:37 pm 
Newbie

Joined: Fri Oct 07, 2005 2:21 pm
Posts: 4
Location: New Brunswick, Canada
I'm in the situation where I'm able to insert records into the the table, but when hibernate tries to retrieve the associated records from the table, it gets stuck in an endless loop reading the same table joined with the other 2.

Is it possible that since we've used hibernate to create our tables versus the old standby of scripts that we may be seeing this issue?


Top
 Profile  
 
 Post subject: Re: mapping composite primary key with foreign key references
PostPosted: Thu Jun 13, 2013 1:28 pm 
Newbie

Joined: Thu Jun 13, 2013 1:22 pm
Posts: 1
Hi

How can relation the field name of table TblItem inside TblInventoryItem?

Using mapping files.


Greetings.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 25 posts ]  Go to page Previous  1, 2

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.