开发者

Getting Hibernate mapping exception with java.util.list

HI, I am getting a problem with a data member of a class. The data member is ContactNumbers. The ContactNumbers is a java.util.list and I am getting errors when hibernate maps the list in the database. What I want is that hibernate creates an array for the contactnumbers in the database postgresql. When I remove the variable contactnumbers of type list from BusinessUser class, the program runs correctly. Can someone helps??

Here is my class:

public class User  
{
    //other varibale contructors setters and getters
}

public class BusinessUser  extends Users implements IsSerializable
{
    List <String> ContactNumbers;
    //other varibale contructors setters and getters
}

My user mapping file is ok. This is my business mapping file

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 2, 2010 4:42:45 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
    <joined-subclass name="com.BiddingSystem.Models.BusinessUser" extends="com.BiddingSystem.Models.Users" table="BUSINESSUSER" lazy="false">
        <key>
            <column name="USERID" />
        </key>
        <property name="BusinessName" type="java.lang.String">
            <column name="BUSINESSNAME" />
        </property>
        <property name="ContactPersonDetails" type="java.lang.String">
            <column name="CONTACTPERSONDETAILS" />
        </property>
        <property name="FaxNumber" type="java.lang.String">
            <column name="FAXNUMBER" />
        </property>
        <list name="ContactNumbers" inverse="false" table="BUSINESSUSER" lazy="true">
            <key>
                <column name="BUSINESSNAME" />
            </key>
            <list-index></list-index>
            <element type="java.lang.String">
                <column name="CONTACTNUMBERS" />
            </element>
        </list>
    </joined-subclass>
</hibernate-mapping>

I am getting this error:

Initial SessionFactory creation failed.org.hibernate.MappingException: Foreign key (FK6214B38B81858749:BUSINESSUSER [BUSINESSNAME])) must have same number of columns as the referenced primary key (BUSINESSUSER [BUSINESSNAME,idx])
[WARN] /biddingsystem/System
java.lang.ExceptionInInitializerError
    at com.BiddingSystem.server.HibernateUtil.<clinit>(HibernateUtil.java:18)
    at com.BiddingSystem.server.ServiceImpl.<init>(ServiceImpl.java:32)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:355)
    at java.lang.Class.newInstance(Class.java:308)
    at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:153)
    at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:463)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:324)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
Caused by: org.hibernate.MappingException: Foreign key (FK6214B38B81858749:BUSINESSUSER [BUSINESSNAME])) must have same number of columns as the referenced primary key (BUSINESSUSER [BUSINESSNAME,idx])
    at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:113)
    at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:96)
    at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1354)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1261)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1377)
    at com.BiddingSystem.server.HibernateUtil.<clinit>(HibernateUtil.java:14)
    ... 26 more
[WARN] Nested in java.lang.ExceptionInInitializerError:
org.hibernate.MappingException: Foreign key (FK6214B38B81858749:BUSINESSUSER [BUSINESSNAME])) must have same number of columns as the referenced primary key (BUSINESSUSER [BUSINESSNAME,idx])
    at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:113)
    at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:96)
    at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1354)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1261)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1377)
    at com.BiddingSystem.server.HibernateUtil.<clinit>(HibernateUtil.java:14)
    at com.BiddingSystem.server.ServiceImpl.<init>(ServiceImpl.java:32)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstan开发者_Python百科ce(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:355)
    at java.lang.Class.newInstance(Class.java:308)
    at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:153)
    at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:463)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:324)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)


You're defining a list of Strings as a collection on your BusinessUser and you're mapping it as if it were a list of Hibernate-mapped entities, which they're not.

So, given the mapping you've defined, Hibernate expects ContactNumbers to be list of BusinessUser (as you've specified: "table='BUSINESSUSER'" for your collection), but in fact it's just a list of Strings.

The solution would be to consider properly defining the ContactNumber entity: is it more then a simple String? If so, does it deserve its own table? If not, wouldn't it be enough to simply add a few fields like contactNumber1, contanctNumber2 on the BusinessUser entity (and also have these as table columns on BUSINESSUSER table)? Is the number of contactNumbers limited for a user?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜