facebook

hibernate mapping approach

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #329289 Reply

    David
    Member

    I am using

      hibernate 3.2.5.ga
      hibernate-annotations 3.3.1.ga
      hibernate-commons-annotations 3.3.0.ga
      spring 3.1.1
      MySQL 5

    This should be a very simple case, but I think I am missing something obvious. I have a User table and a UserRole table – without a foreign key.
    I have the following entities:

    @Entity
    @Table(name = "User", catalog = "mcp", uniqueConstraints = { @UniqueConstraint(columnNames = "UserName") })
    public class User implements Serializable {
    
        // Fields
        @Id
        @Column(name = "UserId", unique = true, nullable = false)
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long userId;
    
        @Column(name = "Status", length = 50)
        private Integer status = 0;
    
        @Column(name = "Password", length = 45)
        private String password;
        @Column(name = "UserName", unique = true, length = 50)
        private String userName;
    
        @OneToOne
        private UserRole userRole;
    .. getters & setters()...
    }

    and

    @Entity
    @Table(name = "UserRole", catalog = "mcp")
    public class UserRole implements Serializable {
    
        @Id
        @Column(name = "Id")
        @GeneratedValue
        private Long id;
    
        @Column(name = "UserRoleDescriptionShort", length = 45)
        private String userRoleDescriptionShort;
    
        @Column(name = "UserRoleDescription", length = 200)
        private String userRoleDescription;
    .. getters & setters()...
    }

    However, when I try to build, I get this:

    Caused by: org.hibernate.HibernateException: Missing column: userRole_Id in mcp.User
    at org.hibernate.mapping.Table.validateColumns(Table.java:254)
    at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1083)
    at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:116)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:317)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
    at com.edelweissco.mcp.dao.DefaultDaoConfig.getSessionFactory(DefaultDaoConfig.java:56)
    at com.edelweissco.mcp.dao.DefaultDaoConfig$$EnhancerByCGLIB$$8a709eb.CGLIB$getSessionFactory$0(<generated>)
    at com.edelweissco.mcp.dao.DefaultDaoConfig$$EnhancerByCGLIB$$8a709eb$$FastClassByCGLIB$$b97b030b.invoke(<generated>)
    at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:167)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:280)
    at com.edelweissco.mcp.dao.DefaultDaoConfig$$EnhancerByCGLIB$$8a709eb.getSessionFactory(<generated>)
    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.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:149)

    So my question is, is this a good strategy for Hibernate/Spring? Should I allow Hibernate to manage the relationships as above, or if I should I modify User table to have a “userRoleId” that holds the value from UserRole table? If so, what would that look like.

    #329292 Reply

    support-swapna
    Moderator

    sonoerin,

    Firstly, do you have a column “Id” in the user role table ? Ideally you should have a foreign key constraint between the two tables.

    It might also be an issue with user permissions. Take a look at this link :
    https://forum.hibernate.org/viewtopic.php?f=1&t=995850

    I suggest you cross post to Hibernate development forums for better support as this is a Hibernate question rather than a MyEclipse one.

    Hope this helps.

    #329346 Reply

    David
    Member

    I should have mentioned I am developing this in MyEclipse:
    Version: 10.5
    Build id: 10.5-20120615

    I have setup a side/test project for mapping of this. Here are the dependencies:

      <spring.version>3.1.1.RELEASE</spring.version>
      <hibernate.version>3.4.0.GA</hibernate.version>
      <mysql.connector.version>5.1.21</mysql.connector.version>
      <hsql.version>1.8.0.10</hsql.version>

    I am just trying to get a unit test going using HSQL for now. Is there a standard way to test Hibernate/Spring ? Each time I create a test I get the previous error message, so I must be doing something wrong, even though this is about as simple of a @OneToOne example as there is (except for the lack of a foreign key).

Viewing 3 posts - 1 through 3 (of 3 total)
Reply To: hibernate mapping approach

You must be logged in to post in the forum log in