-->
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.  [ 6 posts ] 
Author Message
 Post subject: how to auto. create association tables
PostPosted: Fri May 25, 2007 4:59 am 
Newbie

Joined: Fri May 25, 2007 4:42 am
Posts: 13
hi, just try hibernate annotation,
have two classes, Project *-* employees
want to auto create a new association-table to DB, can hibernate annotation do that? or i must manuelly create a such kind of table to DB?

like toturial:
@Entity
public class Store {
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
public Set<Customer> getCustomers() {
...
}
}

@Entity
public class Customer {
@ManyToMany(mappedBy="customers")
public Set<Store> getStores() {
...
}
}

these two classes can created both tables store and customer, and can auto create a asso. table Store_Customer?

have tryed my classes, can not do that, why?
@Entity
@Table(catalog="vipinfo", name="project")

public class Project implements java.io.Serializable {
......
@ManyToMany(
targetEntity=Employee.class,
cascade={CascadeType.PERSIST, CascadeType.MERGE}
)
@JoinTable(
name="PROJECT_EMPLOYEE_REL",
joinColumns={@JoinColumn(name="PROJECT")},
inverseJoinColumns={@JoinColumn(name="EMPLOYEE")}
)

private Collection<Employee> involvedEmployees = new HashSet<Employee>(0);
......}


@Entity
@Table(catalog="vipinfo", name="employee")

public class Employee implements java.io.Serializable {
......
@ManyToMany(
cascade={CascadeType.PERSIST, CascadeType.MERGE},
mappedBy="involvedEmployees",
targetEntity=Project.class)

private Collection<Project> associatedProjects = new HashSet<Project>(0);
......}


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 7:08 am 
Senior
Senior

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

Hibernate can definitely create the association table automatically, as i have done this many times. First thing to check is you hbm2ddl task settings (should be "create" or "update").

Antoher thing worth trying would be, to alter the @JoinColumn annotations in the @JoinTable, so they look something like so

Code:
@JoinTable(
name="PROJECT_EMPLOYEE_REL",
joinColumns={@JoinColumn(name="PROJECT", referencedColumnName="<name of id column>")},
inverseJoinColumns={@JoinColumn(name="EMPLOYEE", referencedColumnName="<name of id column>)}
)


If that does not work, you can try defining your JoinTable on the other side of the relationship (in Employee).

Thanks,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject: fehler:
PostPosted: Fri May 25, 2007 7:33 am 
Newbie

Joined: Fri May 25, 2007 4:42 am
Posts: 13
Caused by: org.hibernate.HibernateException: Missing table: PROJECT_EMPLOYEE_REL

thanx, but does not work,
can u give a example of urs?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 7:44 am 
Newbie

Joined: Fri May 25, 2007 4:42 am
Posts: 13
solved , danke andydale


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 7:52 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
No problem. I have a many to many between student and course like so:

In Course.java
Code:
@Entity
@Table(name = "Course")
@SequenceGenerator(name = "Course_Sequence", allocationSize=1, sequenceName = "course_seq")
public final class Course implements Serializable {
   /**
     * The list of student's enrolled on the course.
     */
    private Set<Student> mEnrolledStudents = new HashSet<Student>();

...........

/**
     * Return the students enrolled on the course.
     * @return mEnrolledStudents student's
     */   
    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable(name = "course_student",
                joinColumns = @JoinColumn(name = "course_id", referencedColumnName = "course_id"),
                inverseJoinColumns = @JoinColumn(name = "student_id", referencedColumnName = "student_id"))   
    public Set<Student> getStudents() {       
        return mEnrolledStudents;
    }


    /**
     * Set the students enrolled on this course.
     * @param pEnrolledStudents students
     */
    public void setStudents(final Set<Student> pEnrolledStudents) {
        this.mEnrolledStudents = pEnrolledStudents;
    }

...........
}


And in Student
Code:
@Entity
@Table(name = "Student")
@SequenceGenerator(name = "Student_Sequence", allocationSize=1, sequenceName = "student_seq")
public final class Student implements Serializable {

    /**
     * Course student is enrolled on
     */
    private Collection<Course> mCourses = new ArrayList<Course>();

.........

   /**
     * Return the courses the student is enrolled on.
     * @return mCourses courses
     */
    @ManyToMany(mappedBy = "students", cascade ={CascadeType.PERSIST, CascadeType.MERGE})
    public Collection<Course> getCourses() {
        return mCourses;
    }
   
    /**
     * See the course the student is enrolled on
     * @param pCoursesEnrolledOn courses
     */
    public void setCourses(final Collection<Course> pCourses) {
        this.mCourses = pCourses;
    }

...........
}


Let me know how you get on

Cheers,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 8:50 am 
Newbie

Joined: Fri May 25, 2007 4:42 am
Posts: 13
1. create and update are both ok for create associated-table
2. referencedColumnName="<name of id column>" is optional, if not write this , use the default primary key (see "is ok")
3. my problem is, in my database i have used many catalog(schema)

for entity entity class, i have written:
@Entity
@Table(catalog="vipinfo", name="project") ///*****
public class Project implements java.io.Serializable {

but i forget it for join-table

public class Project implements java.io.Serializable {

@JoinTable(
catalog="vipinfo", //*** if not write, the talbe will be under default catalog
name="PROJECT_EMPLOYEE_REL",
joinColumns={@JoinColumn(name="PROJECT")}, //is ok
inverseJoinColumns = {@JoinColumn( name="EMPLOYEE")} //is ok
//joinColumns={@JoinColumn(name="PROJECT", referencedColumnName="PRO_ID")},
//inverseJoinColumns = {@JoinColumn( name="EMPLOYEE", referencedColumnName="EMP_ID")}
)

ur tipp encourage me to find my mistake , thanx


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