-->
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: Hibernate Tools and MySql reveng many-to-many problem
PostPosted: Fri Nov 01, 2013 10:04 am 
Newbie

Joined: Fri Nov 01, 2013 9:16 am
Posts: 2
Hi,

I am using Hibernate Tools 3.7.0 Final and MySql 5.6.14 and eclipse Kepler.

I try to reverse engineering to generate pojos form schema:
Code:
CREATE DATABASE  IF NOT EXISTS `test1` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `test1`;
-- MySQL dump 10.13  Distrib 5.6.13, for Win32 (x86)
--
-- Host: 127.0.0.1    Database: test1
-- ------------------------------------------------------
-- Server version   5.6.14

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `a`
--

DROP TABLE IF EXISTS `a`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `a` (
  `id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `a_b`
--

DROP TABLE IF EXISTS `a_b`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `a_b` (
  `id_a` int(11) NOT NULL,
  `id_b` int(11) NOT NULL,
  PRIMARY KEY (`id_a`,`id_b`),
  KEY `FK2_idx` (`id_b`),
  CONSTRAINT `FK1` FOREIGN KEY (`id_a`) REFERENCES `a` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `FK2` FOREIGN KEY (`id_b`) REFERENCES `b` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `b`
--

DROP TABLE IF EXISTS `b`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `b` (
  `id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2013-11-01 14:26:55


pure schema looks like the following:
Code:
CREATE DATABASE  IF NOT EXISTS `test1` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `test1`;

DROP TABLE IF EXISTS `a`;
CREATE TABLE `a` (
  `id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `a_b`;
CREATE TABLE `a_b` (
  `id_a` int(11) NOT NULL,
  `id_b` int(11) NOT NULL,
  PRIMARY KEY (`id_a`,`id_b`),
  KEY `FK2_idx` (`id_b`),
  CONSTRAINT `FK1` FOREIGN KEY (`id_a`) REFERENCES `a` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `FK2` FOREIGN KEY (`id_b`) REFERENCES `b` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `b`;
CREATE TABLE `b` (
  `id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



And here is a picture of that schema:

Image


I am generating POJOs with Eclipse Kepler and Hibernate Tools:

Launch configuration part 1:
Image
Launch configuration exporters:
Image

Hibernate Configuration:

Image

And the output - generated Pojos:

Code:
package it;

// Generated 01.11.2013 14:55:13 by Hibernate Tools 4.0.0


import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

/**
* A generated by hbm2java
*/
@Entity
@Table(name = "a", catalog = "test1")
public class A implements java.io.Serializable {


    private int id;
    private Set<B> bs = new HashSet<B>(0);

    public A() {
    }


    public A(int id) {
        this.id = id;
    }

    public A(int id, Set<B> bs) {
        this.id = id;
        this.bs = bs;
    }

    @Id
    @Column(name = "id", unique = true, nullable = false)
    public int getId() {
        return this.id;
    }

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

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "a_b", catalog = "test1", joinColumns = {@JoinColumn(name = "id_a", nullable = false, updatable = false) },
            inverseJoinColumns = {@JoinColumn(name = "id_b", nullable = false, updatable = false) })
    public Set<B> getBs() {
        return this.bs;
    }

    public void setBs(Set<B> bs) {
        this.bs = bs;
    }


}



Code:
package it;

// Generated 01.11.2013 14:55:13 by Hibernate Tools 4.0.0


import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

/**
* B generated by hbm2java
*/
@Entity
@Table(name = "b", catalog = "test1")
public class B implements java.io.Serializable {


    private int id;
    private Set<A> as = new HashSet<A>(0);

    public B() {
    }


    public B(int id) {
        this.id = id;
    }

    public B(int id, Set<A> as) {
        this.id = id;
        this.as = as;
    }

    @Id
    @Column(name = "id", unique = true, nullable = false)
    public int getId() {
        return this.id;
    }

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

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "a_b", catalog = "test1", joinColumns = {@JoinColumn(name = "id_b", nullable = false, updatable = false) },
            inverseJoinColumns = {@JoinColumn(name = "id_a", nullable = false, updatable = false) })
    public Set<A> getAs() {
        return this.as;
    }

    public void setAs(Set<A> as) {
        this.as = as;
    }


}



Look at the generated @ManyToMany relationship. It does not contain mappedBy attribute in any side of relation. What am I doing wrong? I would like to have this to define owner of relation. How can I achieve that with Hibernate Utils, and even more, if it is possible to do with hibernate maven plugin ?

Code:
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "a_b", catalog = "test1", joinColumns = {@JoinColumn(name = "id_a", nullable = false, updatable = false) },
            inverseJoinColumns = {@JoinColumn(name = "id_b", nullable = false, updatable = false) })
    public Set<B> getBs() {
        return this.bs;
    }

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "a_b", catalog = "test1", joinColumns = {@JoinColumn(name = "id_b", nullable = false, updatable = false) },
            inverseJoinColumns = {@JoinColumn(name = "id_a", nullable = false, updatable = false) })
    public Set<A> getAs() {
        return this.as;
    }


Thanks for help!


Top
 Profile  
 
 Post subject: Re: Hibernate Tools and MySql reveng many-to-many problem
PostPosted: Tue Nov 05, 2013 12:35 pm 
Newbie

Joined: Fri Nov 01, 2013 9:16 am
Posts: 2
Ok, so I made research on my own. It looks like it is bug. I did not search it in jira, but checked source code. In DefaultReverseEngineeringStrategy in method isForeignKeyCollectionInverse always return false for many-to-many relationship. I checked that in version 4.0.0.CR1 this has been fixed.

If someone still wants to use previous versions he must implement his own ReverseEngineeringStrategy, based on the code from DefaultReverseEngineeringStrategy from version 4.0.0.CR1. Or just hardcode the inverse for his schema in isForeignKeyCollectionInverse, which is the simplest (and ugly) solution.

/BR
N


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.