Results 1 to 6 of 6

Thread: ManyToMany doesn't work?

  1. #1
    Join Date
    May 2008
    Posts
    20

    Default ManyToMany doesn't work?

    Hey Gurus!

    I'm not sure what i've done wrong, but ManyToMany relationship doesn't seem to work for me

    here we go:

    Entity1:

    Code:
    Entity
    Table(name = "user_groups")
    public class UserGroup implements Serializable {
    ........
        private List<UserTag> tags;
    ........
        ManyToMany(mappedBy = "userGroups", cascade=CascadeType.ALL)
        public List<UserTag> getTags() {
            return tags;
        }
    btw, I don't know why, but forum doesn't allow me to use AT charachter in my posts ... it thinks i'm trying to post URLs so i've cut them off

    Entity2:
    Code:
    Entity
    Table(name = "user_tags")
    public class UserTag implements Serializable {
    .........
        private List<UserGroup> userGroups = new ArrayList<UserGroup>();
    ..........
        ManyToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE})
        JoinTable(
            name="user_tags_user_groups",
            joinColumns={JoinColumn(name="tags_id")},
            inverseJoinColumns={JoinColumn(name="userGroups_id")}
        )
        public List<UserGroup> getUserGroups() {
            return userGroups;
        }
    And Dao method:

    Code:
      Transactional(propagation = Propagation.REQUIRED)
      public void persistUserGroup(UserGroup ug) {
          if (ug.getId() == null)
              getJpaTemplate().persist( ug );
          else
              getJpaTemplate().merge( ug );
      }
    That's it ... it persists userGroup entity to user_groups table, it persists userTags to user_tags table, but it DOESN'T persist anything to user_tags_user_groups table.. I know it must be something really stupid.

    Here is the log-file:
    Code:
    2008 Jul 08 00:26:42,715 [org.hibernate.cache.UpdateTimestampsCache][DEBUG] Pre-invalidating space [user_groups]
    2008 Jul 08 00:26:42,716 [hibernate.jdbc.util.SQLStatementLogger][DEBUG] insert into user_groups (access, author_id, city, country, createdAt, description, geolocated, modifiedAt, name, region, status) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    2008 Jul 08 00:26:42,719 [org.hibernate.cache.UpdateTimestampsCache][DEBUG] Pre-invalidating space [user_tags]
    2008 Jul 08 00:26:42,721 [hibernate.jdbc.util.SQLStatementLogger][DEBUG] insert into user_tags (name) values (?)
    2008 Jul 08 00:26:42,723 [org.hibernate.cache.UpdateTimestampsCache][DEBUG] Pre-invalidating space [user_tags]
    2008 Jul 08 00:26:42,724 [hibernate.jdbc.util.SQLStatementLogger][DEBUG] insert into user_tags (name) values (?)
    2008 Jul 08 00:26:42,726 [org.hibernate.transaction.JDBCTransaction][DEBUG] commit
    2008 Jul 08 00:26:42,727 [org.hibernate.cache.UpdateTimestampsCache][DEBUG] Pre-invalidating space [user_tags_user_groups]
    2008 Jul 08 00:26:42,727 [org.hibernate.cache.UpdateTimestampsCache][DEBUG] Pre-invalidating space [user_tags_user_groups]
    2008 Jul 08 00:26:42,728 [org.hibernate.cache.UpdateTimestampsCache][DEBUG] Pre-invalidating space [user_tags_user_groups]
    it's obvious it knows about that table, but nothing happens to it apart from these strange chache messages and no joins at insert into user_tags table

  2. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,806

    Default

    btw, I don't know why, but forum doesn't allow me to use AT charachter in my posts ...
    this is a condition of the forum,
    if i dont bad remember i saw in other thread that you nead at least 15 posts to use @

    anyway

    i dont understand very clear your problem, post here your hbm.xml for the many-to-many section

    regards
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3
    Join Date
    May 2008
    Posts
    20

    Default

    Hmm, i don't have hbm.xml

    here is my persistence.xml:
    Code:
    <persistence version="1.0" ... some urls cut off ...>
      <persistence-unit name="rbPU" transaction-type="RESOURCE_LOCAL">
        <non-jta-data-source/>
    ..... some classes skipped .....
    <class>model.usergroups.UserGroup</class>
      <class>model.usergroups.UserTag</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
      </persistence-unit>
    </persistence>
    Here is an excerpt from applicationContext.xml:

    Code:
    <bean id="persistenceUnitManager" 
              class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
            <property name="persistenceXmlLocations">
                <list>
                    <value>classpath:META-INF/persistence.xml</value>
                </list>
            </property>
            <property name="defaultDataSource" ref="dataSource"/>
        </bean>
    
        <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
    
        <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    
    <bean id="entityManagerFactory" 
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceUnitManager" ref="persistenceUnitManager"/>
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="database" value="HSQL" />
                 </bean>
            </property>
            <property name="jpaProperties">
                    <props>
                        <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                        <prop key="hibernate.show_sql">true</prop>
                        <prop key="hibernate.format_sql">false</prop>
                        <prop key="hibernate.hbm2ddl.auto">false</prop>
                        <prop key="hibernate.transaction.flush_before_completion">false</prop>
                        <prop key="hibernate.transaction.auto_close_session">false</prop>
                        <prop key="hibernate.connection.release_mode">after_transaction</prop>
                        <prop key="hibernate.cache.use_query_cache">true</prop> 
                        <prop key="hibernate.connection.aggressive_release">false</prop>
                        <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                    </props>
            </property>
        </bean>

    is that what you're asking for?

  4. #4
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,806

    Default

    is that what you're asking for?
    no yet, but for UserGroup and UserTag , for both pojo classes where is the
    UserGroup.hbm.xml,UserTag.hbm.xml files used by Hibernate?

    i am asking this, coz i never work before with Annotations

    regards
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  5. #5
    Join Date
    May 2008
    Posts
    20

    Default

    hm, there are no hbm files, coz i never worked with them )))

    here is sql for creating tables (MySql):

    Code:
    CREATE TABLE user_groups (
    	 id 	 BIGINT AUTO_INCREMENT PRIMARY KEY,
    	 access   TINYINT NOT NULL,
    	 status   TINYINT NOT NULL,
    	 region  INT,
    	 country  INT,
    	 city		INT,
    	 createdAt   DATETIME NOT NULL,
    	 modifiedAt   DATETIME,
    	 author_id   BIGINT NOT NULL,
    	 name	 VARCHAR(255) NOT NULL,
    	 description VARCHAR(2047) NOT NULL,
    	 geolocated TINYINT NOT NULL
     ) CHARSET=UTF8;
     
     CREATE TABLE user_tags (
    	 id 	 BIGINT AUTO_INCREMENT PRIMARY KEY,
    	 name	 VARCHAR(255) NOT NULL UNIQUE
     ) CHARSET=UTF8;
     
     CREATE TABLE user_tags_user_groups (
    	 tags_id bigint(20) NOT NULL,
    	 userGroups_id bigint(20) NOT NULL,
    	 KEY FK708B7B96F5DEFBB3 (tags_id),
    	 KEY FK708B7B96107B192 (userGroups_id)
     );
    All I have relating to the topic is this SQL, POJO-Objects and XML-files: applicationContext.xml & persistence.xml. Perhaps I need to add some hibernate settings in jpaProperties section ...
    Last edited by Leo++; Jul 8th, 2008 at 10:18 AM.

  6. #6
    Join Date
    May 2008
    Posts
    20

    Default

    Ok, I got it eventually. I have changed the direction ... moved mappedBy to UserTag and JoinTable annotation to UserGroup entity. Everythig has just started working. Thanks for your attention

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •