-->
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.  [ 1 post ] 
Author Message
 Post subject: @ManyToMany Annotation Mapping Problem
PostPosted: Fri Oct 29, 2010 2:31 pm 
Newbie

Joined: Fri Oct 29, 2010 2:16 pm
Posts: 1
I'm trying to get a ManyToMany mapping working with security database and am encountering a problem that I do not know how to resolve.

Here's my code:

Code:
@Entity
@Table(name = "sec_DBGroup")
public class DBGroup {
    private long id;

    @Column(name = "Id", nullable = false, insertable = true, updatable = true, length = 19, precision = 0)
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    public long getId() {
        return id;
    }

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

    private String name;

    @Column(name = "Name", nullable = false, insertable = true, updatable = true, length = 128, precision = 0)
    @Basic
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String description;

    @Column(name = "Description", nullable = true, insertable = true, updatable = true, length = 128, precision = 0)
    @Basic
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        DBGroup dbgroup = (DBGroup) o;

        if (id != dbgroup.id) return false;
        if (name != null ? !name.equals(dbgroup.name) : dbgroup.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (id ^ (id >>> 32));
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }

    private Collection<SecurityRole> roles;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(
            name = "sec_DBGroupRoleMap",
            joinColumns = @JoinColumn(name = "DBGroupId"),
            inverseJoinColumns = @JoinColumn(name = "SecurityRoleId")
    )
    @ForeignKey(name = "FK_DBGroupRoleMap_DBGroup", inverseName = "FK_DBGroupRoleMap_SecurityRole")
    @CollectionId(
            columns = @Column(name="Id"),
            type = @Type(type = "long"),
            generator = "native"
    )
    public Collection<SecurityRole> getRoles() {
        return roles;
    }

    public void setRoles(Collection<SecurityRole> roles) {
        this.roles = roles;
    }

    private Collection<SystemUser> users;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(
            name = "sec_UserDBGroupMap",
            joinColumns = @JoinColumn(name = "DBGroupId"),
            inverseJoinColumns = @JoinColumn(name = "SystemUserId")
    )
    @ForeignKey(name = "FK_UserDBGroupMap_DBGroup", inverseName = "FK_UserDBGroupMap_SystemUser")
    @CollectionId(
            columns = @Column(name="Id"),
            type = @Type(type = "long"),
            generator = "native"
    )
    public Collection<SystemUser> getUsers() {
        return users;
    }

    public void setUsers(Collection<SystemUser> users) {
        this.users = users;
    }
}

@Entity
@Table(name = "sec_DBGroupRoleMap")
public class DBGroupRoleMap {
    private long id;

    @Column(name = "Id", nullable = false, insertable = true, updatable = true, length = 19, precision = 0)
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    public long getId() {
        return id;
    }

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

    private long dbgroupid;

    @Column(name = "DBGroupId", nullable = false, insertable = true, updatable = true, length = 19, precision = 0)
    @Basic
    public long getDbgroupid() {
        return dbgroupid;
    }

    public void setDbgroupid(long dbgroupid) {
        this.dbgroupid = dbgroupid;
    }

    private long securityroleid;

    @Column(name = "SecurityRoleId", nullable = false, insertable = true, updatable = true, length = 19, precision = 0)
    @Basic
    public long getSecurityroleid() {
        return securityroleid;
    }

    public void setSecurityroleid(long securityroleid) {
        this.securityroleid = securityroleid;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        DBGroupRoleMap that = (DBGroupRoleMap) o;

        if (dbgroupid != that.dbgroupid) return false;
        if (id != that.id) return false;
        if (securityroleid != that.securityroleid) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (id ^ (id >>> 32));
        result = 31 * result + (int) (dbgroupid ^ (dbgroupid >>> 32));
        result = 31 * result + (int) (securityroleid ^ (securityroleid >>> 32));
        return result;
    }

}

@Entity
@Table(name = "sec_SecurityRole")
public class SecurityRole {
    @Expose
    private long id;

    @Column(name = "Id", nullable = false, insertable = true, updatable = true, length = 19, precision = 0)
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    public long getId() {
        return id;
    }

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

    @Expose
    private String name;

    @Column(name = "Name", nullable = false, insertable = true, updatable = true, length = 128, precision = 0)
    @Basic
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String description;

    @Column(name = "Description", nullable = true, insertable = true, updatable = true, length = 128, precision = 0)
    @Basic
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        SecurityRole that = (SecurityRole) o;

        if (id != that.id) return false;
        if (description != null ? !description.equals(that.description) : that.description != null) return false;
        if (name != null ? !name.equals(that.name) : that.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (id ^ (id >>> 32));
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        return result;
    }

    private Collection<SecurityPermission> permissions;

    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(
            name = "sec_RolePermissionMap",
            joinColumns = @JoinColumn(name = "SecurityRoleId"),
            inverseJoinColumns = @JoinColumn(name = "SecurityPermissionId")
    )
    @ForeignKey(name = "FK_RolePermissionMap_SecurityRole", inverseName = "FK_RolePermissionMap_SecurityPermission")
    @CollectionId(
            columns = @Column(name="Id"),
            type=@Type(type="long"),
            generator = "identity"
    )
    public Collection<SecurityPermission> getPermissions() {
        return permissions;
    }

    public void setPermissions(Collection<SecurityPermission> permissions) {
        this.permissions = permissions;
    }

    private Collection<DBGroup> dbgroups;

    @ManyToMany(cascade = CascadeType.ALL, mappedBy = "roles")
    public Collection<DBGroup> getDbgroups() {
        return dbgroups;
    }

    public void setDbgroups(Collection<DBGroup> dbgroups) {
        this.dbgroups = dbgroups;
    }
}


When I try to create the database I get an exception:

Code:
13:23:43.751 INFO  main NullableType.nullSafeSet - could not bind value 'POST_INSERT_INDICATOR' to parameter: 2; org.hibernate.id.IdentifierGeneratorHelper$2 cannot be cast to java.lang.Long
Oct 29, 2010 1:23:43 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class com.vitalimages.common.server.listener.DatabaseUpgradeListener
java.lang.ClassCastException: org.hibernate.id.IdentifierGeneratorHelper$2 cannot be cast to java.lang.Long
   at org.hibernate.type.LongType.set(LongType.java:65)
   at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:156)
   at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:138)
   at org.hibernate.persister.collection.AbstractCollectionPersister.writeIdentifier(AbstractCollectionPersister.java:868)
   at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1199)
   at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:260)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:183)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
   at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
   at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:407)
   at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
   at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:737)
   at com.vitalimages.common.server.domain.SecurityDAO.createDBGroup(SecurityDAO.java:95)
   at com.vitalimages.common.server.listener.DatabaseUpgradeListener.contextInitialized(DatabaseUpgradeListener.java:182)
   at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4135)
   at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)
   at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
   at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
   at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
   at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
   at org.apache.catalina.startup.Embedded.start(Embedded.java:825)
   at org.codehaus.mojo.tomcat.AbstractRunMojo.startContainer(AbstractRunMojo.java:558)
   at org.codehaus.mojo.tomcat.AbstractRunMojo.execute(AbstractRunMojo.java:255)
   at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
   at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
   at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
   at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
   at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
   at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
   at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
   at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
   at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
   at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
   at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
   at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
   at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
   at org.codehaus.classworlds.Launcher.main(Launcher.java:375)


I've tried changing the generator to various other incarnations (native vs increment):

Code:
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(
            name = "sec_DBGroupRoleMap",
            joinColumns = @JoinColumn(name = "DBGroupId"),
            inverseJoinColumns = @JoinColumn(name = "SecurityRoleId")
    )
    @ForeignKey(name = "FK_DBGroupRoleMap_DBGroup", inverseName = "FK_DBGroupRoleMap_SecurityRole")
    @CollectionId(
            columns = @Column(name="Id"),
            type = Type(type = "long"),
            generator = "increment"
    )


specifying a named generic generator:

Code:
   @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(
            name = "sec_DBGroupRoleMap",
            joinColumns = @JoinColumn(name = "DBGroupId"),
            inverseJoinColumns = @JoinColumn(name = "SecurityRoleId")
    )
    @ForeignKey(name = "FK_DBGroupRoleMap_DBGroup", inverseName = "FK_DBGroupRoleMap_SecurityRole")
    @GenericGenerator(name = "dbg_generator",strategy = "native")
    @CollectionId(
            columns = @Column(name="Id"),
            type = Type(type = "long"),
            generator = "dbg_generator"
    )
    public Collection<SecurityRole> getRoles() {
        return roles;
    }


I'm using hibernate 3.5.3 for this project. Anyone have any suggestions? I've read a ton of other people asking somewhat similar questions but don't see anyone posting responses that solve the problem.

viewtopic.php?f=1&t=983523
viewtopic.php?f=1&t=981797&start=0

Thanks,

Grant


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.