-->
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.  [ 4 posts ] 
Author Message
 Post subject: ManyToMany and creating schema
PostPosted: Tue Jan 16, 2007 1:48 pm 
Newbie

Joined: Tue Jan 16, 2007 1:26 pm
Posts: 5
Location: Vienna
Hibernate version: 3.2.1 GA
Database version: MySQL 5
The generated SQL :
create table manypartone (id integer not null auto_increment, twos tinyblob, primary key (id))
create table manyparttwo (id integer not null auto_increment, ones tinyblob, primary key (id))


Hello,

I'm trying to use a ManyToMany assocation with EJB3 persistance, but my code doesn't create a association table. I have tried to create a simple example. You can see that instead of a join table two tinyblobs are generated. I have replaced MySqlDialect with MySqlDialect5 and HSQLDialect with the same result. Is this result intended and how is it possible to change this to a join table? I doesn't see big differences between my code and the examples.

Code:
import java.util.*;
import javax.persistence.*;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class Main {

   @Entity
   @Table(name="manyparttwo")
   public static class ManyPartTwo {

      @Id
      @GeneratedValue
      private Integer id;
      
      private TreeSet<ManyPartOne> ones = new TreeSet<ManyPartOne>();
      
      @ManyToMany(
            targetEntity=ManyPartOne.class,
            cascade=CascadeType.PERSIST,
            mappedBy="twos"
      )
      public TreeSet<ManyPartOne> getOnes() {
         return ones;
      }

      public void TreeSetOnes(TreeSet<ManyPartOne> ones) {
         this.ones = ones;
      }

      public Integer getId() {
         return id;
      }

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

   @Entity
   @Table(name="manypartone")
   public static class ManyPartOne {

      @Id
      @GeneratedValue
      private Integer id;
      
      private TreeSet<ManyPartTwo> twos = new TreeSet<ManyPartTwo>();

      @ManyToMany(
            
            targetEntity=ManyPartTwo.class,
            cascade=CascadeType.PERSIST
      )
      @JoinTable(
            name="many2many",
            joinColumns={@JoinColumn(name="one")},
            inverseJoinColumns={@JoinColumn(name="two")}
      )
      public TreeSet<ManyPartTwo> getTwos() {
         return twos;
      }

      public void TreeSetTwos(TreeSet<ManyPartTwo> twos) {
         this.twos = twos;
      }
      
      public Integer getId() {
         return id;
      }

      public void TreeSetId(Integer id) {
         this.id = id;
      }
   }
   
   public static void main(String[] args) {
      AnnotationConfiguration cfg = new AnnotationConfiguration();
      cfg.addAnnotatedClass(Main.ManyPartOne.class);
      cfg.addAnnotatedClass(Main.ManyPartTwo.class);
      cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
      
      new SchemaExport(cfg).create(true, false);
      
   }
   
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 17, 2007 4:18 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

Try defining the join table in the class ManyPartOne like so:
Code:
@JoinTable(name = "many2many",
                joinColumns = @JoinColumn(name = "one", referencedColumnName = "<name  of id column of ManyPartOne>"),
                inverseJoinColumns = @JoinColumn(name = "two", referencedColumnName = "<name of id column of ManyPartTwo>"))


Cheers,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject: No success
PostPosted: Wed Jan 17, 2007 5:12 am 
Newbie

Joined: Tue Jan 16, 2007 1:26 pm
Posts: 5
Location: Vienna
Hi,
I have added "referencedColumnName", but I get the same create statements :-(

<code>
@JoinTable(
name="many2many",
joinColumns={@JoinColumn(name="one", referencedColumnName="id")},
inverseJoinColumns={@JoinColumn(name="two", referencedColumnName="id")}
)
</code>


Top
 Profile  
 
 Post subject: Solved
PostPosted: Wed Jan 17, 2007 6:02 am 
Newbie

Joined: Tue Jan 16, 2007 1:26 pm
Posts: 5
Location: Vienna
Now I know what I have done wrong. I have mixed annotations at declaration level and getter/setter method level
The "id" was set at declaration level while my manyToMany relation was annotated at getter method.

Many thanks to andydale again.

Mike


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