Results 1 to 15 of 15
  1. #1
    javadev is offline Member
    Join Date
    Jun 2007
    Posts
    14
    Rep Power
    0

    Smile composite-id problem hibernate

    hello frnds,

    i am new to hibernate...

    i have an problem with composite key mapping in hibernate....

    if any one has idea about this then please reply me as early as possible...

    thanx in advance...

    regards..
    javadev

  2. #2
    Heather is offline Senior Member
    Join Date
    Jun 2007
    Posts
    164
    Rep Power
    8

    Default

    What kind of problem do you have?
    paste the code and the error or exception that appears in your program
    ;)

  3. #3
    javadev is offline Member
    Join Date
    Jun 2007
    Posts
    14
    Rep Power
    0

    Default

    thanx for reply

    just chk my condition..

    i have one table with four primary key....

    i just write following code in mapping file...

    <composite-id>
    <key-property name="countryCode" column="countyCd"/>
    <key-property name="dtdVer" column="dtdver"/>
    .
    .
    </composite-id>

    and i just fetch record from table by criteria query..

    is it neccessory to override equals() and hashCode() for this condn.?
    pls help me..

    thanx

  4. #4
    Peter is offline Senior Member
    Join Date
    Jun 2007
    Posts
    119
    Rep Power
    0

    Default

    is it necessary to override equals() and hashCode() for this condition?
    what is the condition? and what do you want to override?

  5. #5
    javadev is offline Member
    Join Date
    Jun 2007
    Posts
    14
    Rep Power
    0

    Default

    hello frnd,

    i give more about my prblm...
    this is code i fetch record...
    //--------------
    xml_config xConfig = new xml_config();
    Criteria cri = session.createCriteria(xml_config.class);
    String s="CA";
    //Query q = session.createQuery("from xml_config config where config.country_cd='CA'");
    cri.add(Restrictions.like("country_cd", s));
    cri.add(Restrictions.like("Dtd_version", "1.0"));
    List q = cri.list();//error line by eclipse ide
    Iterator itr = q.iterator();
    ArrayList<String> v = new ArrayList<String>();
    while(itr.hasNext())
    {
    xConfig = (zespl_xml_config)itr.next();
    v.add(xConfig.getDtd_FileName());
    System.out.println("ICH FLAG "+xConfig.getICHFlag());
    System.out.println("Dtd file Name "+xConfig.getDtd_FileName());
    }
    //----------
    and this my 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">
    <hibernate-mapping>
    <class name="HibernateUtil.zespl_xml_config" table="zespl_gifnoc_lmx">
    <composite-id>
    <key-property name="country_cd" column="dc_yrtnuoc"/>
    <key-property name="Dtd_type" column="epyt_dtd"/>
    <key-property name="Dtd_version" column="noisrev_dtd"/>
    <key-property name="Dtd_FileName" column="eman_elif_dtd"/>
    </composite-id>
    <property name="ICHFlag" column="galf_hci"/>
    <property name="xls_FileName" column="eman_elif_lsx"/>
    </class>
    </hibernate-mapping>
    //------------------

    and the output errror is ......

    //--------------------------

    Hibernate: select this_.dc_yrtnuoc as dc1_0_, this_.epyt_dtd as epyt2_0_, this_.noisrev_dtd as noisrev3_0_, this_.eman_elif_dtd as eman4_0_, this_.dc_yrtnuoc as dc1_1_0_, this_.epyt_dtd as epyt2_1_0_, this_.noisrev_dtd as noisrev3_1_0_, this_.eman_elif_dtd as eman4_1_0_, this_.galf_hci as galf5_1_0_, this_.eman_elif_lsx as eman6_1_0_ from zespl_gifnoc_lmx this_ where this_.dc_yrtnuoc like ? and this_.noisrev_dtd like ?
    java.lang.ClassCastException: HibernateUtil.xml_config
    at org.hibernate.loader.Loader.getKeyFromResultSet(Lo ader.java:650)
    at org.hibernate.loader.Loader.getRowFromResultSet(Lo ader.java:277)
    at org.hibernate.loader.Loader.doQuery(Loader.java:38 4)
    at org.hibernate.loader.Loader.doQueryAndInitializeNo nLazyCollections(Loader.java:203)
    at org.hibernate.loader.Loader.doList(Loader.java:149 9)
    at org.hibernate.loader.Loader.list(Loader.java:1482)
    at org.hibernate.loader.criteria.CriteriaLoader.list( CriteriaLoader.java:111)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.ja va:1246)
    at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl. java:299)
    at HibernateDemo.Main.main(Main.java:87)

    and my java bean is...
    //---
    public class xml_config
    {
    String country_cd;
    String ICHFlag;
    String Dtd_type;
    String Dtd_version;
    String Dtd_FileName;
    String xls_FileName;
    public String getCountry_cd() {
    return country_cd;
    }
    public void setCountry_cd(String country_cd) {
    this.country_cd = country_cd;
    }
    public String getDtd_FileName() {
    return Dtd_FileName;
    }
    public String getDtd_type() {
    return Dtd_type;
    }
    public String getDtd_version() {
    return Dtd_version;
    }
    public String getICHFlag() {
    return ICHFlag;
    }
    public String getXls_fileName() {
    return xls_FileName;
    }
    public void setDtd_FileName(String dtd_FileName) {
    Dtd_FileName = dtd_FileName;
    }
    public void setDtd_type(String dtd_type) {
    Dtd_type = dtd_type;
    }
    public void setDtd_version(String dtd_version) {
    Dtd_version = dtd_version;
    }
    public void setICHFlag(String flag) {
    ICHFlag = flag;
    }
    public String getXls_FileName() {
    return xls_FileName;
    }
    public void setXls_FileName(String xls_FileName) {
    this.xls_FileName = xls_FileName;
    }

    }
    //---

    if u have solution then please help me...

    thanx and regards,
    JavaDev84

  6. #6
    Heather is offline Senior Member
    Join Date
    Jun 2007
    Posts
    164
    Rep Power
    8

    Default

    First: what zespl_xml_config is? Is it an inner class?
    I don't understand why the class is defined like that HibernateUtil.zespl_xml_config

    Second: check this fields
    Name: java notation
    country_cd -> countryCd
    Dtd_type -> dtdType
    Dtd_version-> dtdVersion
    Dtd_FileName-> dtdFileName

    This notations are very important,they are standardization.
    Hibernate uses reflection to access and sets the values of your fields
    you should use this standardization to the names of the fields, so when you run hibernate maybe you don't find exceptions an errors when hibernate tries to set the names of the fields
    ;)

  7. #7
    javadev is offline Member
    Join Date
    Jun 2007
    Posts
    14
    Rep Power
    0

    Default

    hello frnd,
    thanx for u r valuable guidence..

    as per u r suggation i change the naming notation...
    but problem again same..

    when i use simple code without <composite-id> it works well..
    but when i use composite id it gives exception like above...
    Just tell me that what are conditions to use <composite-id>
    1. is it necessory to write <key-many-to-one>?
    2. is it necessory to override equals(),hashCode() and implements Serializable?

    please help me..
    again i give u my code that i use...
    //mapping file
    <class name="HibernateUtil.xmlConfig" table="zespl_gifnoc_lmx">
    <composite-id>
    <key-property name="countryCd" column="dc_yrtnuoc"/>
    <key-property name="dtdVersion" column="noisrev_dtd"/>
    <key-property name="dtdType" column="epyt_dtd"/>
    <key-property name="dtdFileName" column="eman_elif_dtd"/>
    </composite-id>
    <property name="xlsFileName" column="eman_elif_lsx"/>
    <property name="ICHFlag" column="galf_hci"/>
    </class>

    //cong.cfg.xml
    <hibernate-configuration>
    <session-factory>
    <property name="hibernate.connection.driver_class">net.sourc eforge.jtds.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:jtds:sqlserve r://192.168.1.64:1433/Dossier-Mgmt</property>
    <property name="hibernate.connection.username">sa</property>
    <property name="hibernate.connection.password"></property>
    <property name="dialect">org.hibernate.dialect.SQLServerDial ect</property>
    <property name="show_sql">true</property>
    <mapping resource="HibernateDemo\xmlConfig.hbm.xml"/>
    </session-factory>

    //error log
    INFO - Hibernate 3.0rc1
    INFO - hibernate.properties not found
    INFO - using CGLIB reflection optimizer
    INFO - using JDK 1.4 java.sql.Timestamp handling
    INFO - configuring from resource: /hibernate.cfg.xml
    INFO - Configuration resource: /hibernate.cfg.xml
    INFO - Mapping resource: HibernateDemo\xmlConfig.hbm.xml
    INFO - Mapping class: HibernateDemo.xmlConfig -> zespl_gifnoc_lmx
    WARN - Could not perform validation checks for component as the class HibernateDemo.xmlConfig was not found
    INFO - Configured SessionFactory: null
    INFO - processing extends queue
    INFO - processing collection mappings
    INFO - processing association property references
    INFO - processing foreign key constraints
    INFO - Using dialect: org.hibernate.dialect.SQLServerDialect
    INFO - Default batch fetch size: 1
    INFO - Generate SQL with comments: disabled
    INFO - Order SQL updates by primary key: disabled
    INFO - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
    INFO - Using ASTQueryTranslatorFactory
    INFO - Query language substitutions: {}
    INFO - Using Hibernate built-in connection pool (not for production use!)
    INFO - Hibernate connection pool size: 20
    INFO - autocommit mode: false
    INFO - using driver: net.sourceforge.jtds.jdbc.Driver at URL: jdbc:jtds:sqlserver://192.168.1.64:1433/Dossier-Mgmt
    INFO - connection properties: {user=sa, password=****}
    INFO - Scrollable result sets: enabled
    INFO - JDBC3 getGeneratedKeys(): enabled
    INFO - Using default transaction strategy (direct JDBC transactions)
    INFO - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
    INFO - Automatic flush during beforeCompletion(): disabled
    INFO - Automatic session close at end of transaction: disabled
    INFO - Cache provider: org.hibernate.cache.EhCacheProvider
    INFO - Second-level cache: enabled
    INFO - Optimize cache for minimal puts: disabled
    INFO - Structured second-level cache entries: enabled
    INFO - Query cache: disabled
    INFO - Echoing all SQL to stdout
    INFO - Statistics: disabled
    INFO - Deleted entity synthetic identifier rollback: disabled
    INFO - Default entity-mode: pojo
    INFO - building session factory
    WARN - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C://lib/ehcache-1.1.jar!/ehcache-failsafe.xml
    INFO - Not binding factory to JNDI, no JNDI name configured
    INFO - Checking 0 named queries
    Hibernate: select xmlconfig0_.dc_yrtnuoc as col_0_0_, xmlconfig0_.noisrev_dtd as col_0_1_, xmlconfig0_.epyt_dtd as col_0_2_, xmlconfig0_.eman_elif_dtd as col_0_3_ from zespl_gifnoc_lmx xmlconfig0_
    Exception in thread "main" java.lang.ClassCastException: HibernateDemo.xmlConfig
    at org.hibernate.type.ManyToOneType.hydrate(ManyToOne Type.java:63)
    at org.hibernate.type.EntityType.nullSafeGet(EntityTy pe.java:196)
    at org.hibernate.impl.IteratorImpl.postNext(IteratorI mpl.java:95)
    at org.hibernate.impl.IteratorImpl.<init>(IteratorImp l.java:62)
    at org.hibernate.loader.hql.QueryLoader.iterate(Query Loader.java:382)
    at org.hibernate.hql.ast.QueryTranslatorImpl.iterate( QueryTranslatorImpl.java:278)
    at org.hibernate.impl.SessionImpl.iterate(SessionImpl .java:865)
    at org.hibernate.impl.QueryImpl.iterate(QueryImpl.jav a:41)
    at HibernateDemo.TestXmlConfig.main(TestXmlConfig.jav a:14)

    can WARN matters for execution of code????
    thanx and regards,
    JavaDev48

  8. #8
    Jack is offline Senior Member
    Join Date
    Jun 2007
    Posts
    130
    Rep Power
    0

    Default

    In your mapping you declared:
    <class name="HibernateUtil.zespl_xml_config" table="zespl_gifnoc_lmx">

    and your the name of your class is:
    public class xml_config

    maybe this is the problem

  9. #9
    javadev is offline Member
    Join Date
    Jun 2007
    Posts
    14
    Rep Power
    0

    Default

    hi frnd,

    if u can seen my new mapping i use HibernateUtil.xmlConfig and my java bean class name is same i can't post here...

    the problem is diffrent... the code work well without composite-id but why not this problem get solved..

    if u have another example that works for composite-id pls help me.

    thanx and regards
    Dev48

  10. #10
    javadev is offline Member
    Join Date
    Jun 2007
    Posts
    14
    Rep Power
    0

    Default

    hi frnd

    as u seen my new mapping file i give xmlConfig that is my bean class name.

    problem is diffrent.. becoz the code work without compositeid...

    but why not with composite-id

    if u have any another example code that work for composite-id pls help me,,,

    thankx and regards
    Javadev48

  11. #11
    Peter is offline Senior Member
    Join Date
    Jun 2007
    Posts
    119
    Rep Power
    0

    Default

    You have to do your class as "serializable" and re implement the method equals and hashcode
    I recommend you this documentation, it's easy to understand and it has examples
    HIBERNATE - Relational Persistence for Idiomatic Java

  12. #12
    gina.nguyen is offline Member
    Join Date
    Jan 2008
    Posts
    1
    Rep Power
    0

    Default that document seems not very clear...

    Hi,

    I "google" the "composite id" and one of the link is that document. After reading it, I implemented my composite id as the primary key of a table (there is only 1 table). The rows in the table are identified uniquely by 5 columns of type varchar2 (or String in Java). Here is my implementation:

    <?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 Oct 17, 2007 4:12:40 PM by Hibernate Tools 3.2.0.b9 -->
    <hibernate-mapping>

    <class name="gain.site.server.persistence.InvLoc" table="XX_INV_LOCATION_V">
    <composite-id name="invLocPK" class="gain.site.server.persistence.InvLocPK" mapped="true">
    <key-property name="supPartNum" column="ITEM_NUMBER" type="string" length="10" mapped="true" />
    <key-property name="partSN" column="SERIAL_NUMBER" type="string" length="10" mapped="true" />
    <key-property name="suppCode" column="SUPPLIER_CODE" type="string" length="10" mapped="true" />
    <key-property name="custRefID" column="CUST_REF_ID" type="string" length="10" mapped="true" />
    <key-property name="locCode" column="LOCATION_NAME" type="string" length="10" mapped="true" />
    </composite-id>
    <property name="supPartNum" type="string">
    <column name="ITEM_NUMBER" length="10" />
    </property>
    <property name="partSN" type="string">
    <column name="SERIAL_NUMBER" length="10" />
    </property>
    <property name="suppCode" type="string">
    <column name="SUPPLIER_CODE" length="10" />
    </property>
    <property name="custRefID" type="string">
    <column name="CUST_REF_ID" length="10" />
    </property>
    <property name="locCode" type="string">
    <column name="LOCATION_NAME" length="10" />
    </property>


    <property name="quantity" type="integer">
    <column name="QUANTITY" precision="22" scale="0" />
    </property>
    <property name="UOM" type="string">
    <column name="UNIT_OF_MEASURE_CODE" length="10" />
    </property>
    </class>

    </hibernate-mapping>

    public class InvLocPK implements Serializable {
    /**
    *
    */
    private static final long serialVersionUID = 9L;
    private String supPartNum;
    private String partSN;
    private String suppCode;
    private String custRefID;
    private String locCode;

    public InvLocPK() {
    }

    public InvLocPK(InvLocPK invLocPK) {
    this.suppCode = invLocPK.suppCode;
    this.partSN = invLocPK.partSN;
    this.locCode = invLocPK.locCode;
    this.custRefID = invLocPK.custRefID;
    this.supPartNum = invLocPK.supPartNum;
    }

    public boolean equals(InvLocPK invLocPK) {
    return (this.custRefID.equals(invLocPK.custRefID) &&
    this.locCode.equals(invLocPK.locCode) &&
    this.partSN.equals(invLocPK.partSN) &&
    this.supPartNum.equals(invLocPK.supPartNum) &&
    this.suppCode.equals(invLocPK.suppCode));
    }

    public boolean equals(Object obj) {
    if(this == obj)
    return true;
    if((obj == null) || (obj.getClass() != this.getClass()))
    return false;
    // object must be Test at this point
    InvLocPK invLocPK = (InvLocPK)obj;
    return (this.custRefID.equals(invLocPK.custRefID) &&
    this.locCode.equals(invLocPK.locCode) &&
    this.partSN.equals(invLocPK.partSN) &&
    this.supPartNum.equals(invLocPK.supPartNum) &&
    this.suppCode.equals(invLocPK.suppCode));
    }

    public int hashCode () {
    return new HashCodeBuilder().
    append(getSuppCode()).
    append(getPartSN()).
    append(getSupPartNum()).
    append(getLocCode()).
    append(getCustRefID()).
    toHashCode();
    }

    public String getSupPartNum() {
    return supPartNum;
    // return "A-1";
    }

    public void setSupPartNum(String supPartNum) {
    this.supPartNum = supPartNum;
    }

    public String getPartSN() {
    return partSN;
    // return "501";
    }

    public void setPartSN(String partSN) {
    this.partSN = partSN;
    }

    public String getSuppCode() {
    return suppCode;
    // return "C11111";
    }

    public void setSuppCode(String suppCode) {
    this.suppCode = suppCode;
    }

    public String getCustRefID() {
    return custRefID;
    // return "GCSRT";
    }

    public void setCustRefID(String custRefID) {
    this.custRefID = custRefID;
    }

    public String getLocCode() {
    return locCode;
    // return "W101";
    }

    public void setLocCode(String locCode) {
    this.locCode = locCode;
    }

    } // end of InvLocPK class


    public class InvLoc {
    private InvLocPK invLocPK;
    private int quantity;
    private String UOM;

    public InvLoc() {
    invLocPK = new InvLocPK();
    }

    public String getSupPartNum() {
    return this.invLocPK.getSupPartNum();
    // return "A-1";
    }

    public void setSupPartNum(String supPartNum) {
    this.invLocPK.setSupPartNum(supPartNum);
    }

    public String getPartSN() {
    return this.invLocPK.getPartSN();
    // return "501";
    }

    public void setPartSN(String partSN) {
    this.invLocPK.setPartSN(partSN);
    }

    public String getSuppCode() {
    return this.invLocPK.getSuppCode();
    // return "C11111";
    }

    public void setSuppCode(String suppCode) {
    this.invLocPK.setSuppCode(suppCode);
    }

    public String getCustRefID() {
    return this.invLocPK.getCustRefID();
    // return "GCSRT";
    }

    public void setCustRefID(String custRefID) {
    this.invLocPK.setCustRefID(custRefID);
    }

    public String getLocCode() {
    return this.invLocPK.getLocCode();
    // return "W101";
    }

    public void setLocCode(String locCode) {
    this.invLocPK.setLocCode(locCode);
    }

    public InvLocPK getInvLocPK() {
    return invLocPK;
    }

    public void setInvLocPK(InvLocPK invLocPK) {
    this.invLocPK = invLocPK;
    }

    public int getQuantity() {
    return quantity;
    // return 1;
    }

    public void setQuantity(int quantity) {
    this.quantity = quantity;
    }

    public String getUOM() {
    return UOM;
    // return "EA";
    }

    public void setUOM(String uom) {
    UOM = uom;
    }

    }

    I got mapping exception. What could be wrong with my implementation?

    Thanks,
    Gina

  13. #13
    rhlpnt is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default can u help me...

    i am trying to insert the value in databases but it is fired by hibernate...
    i have menationed all detail here

    Hibernate: select unwanted0_.ID as ID0_0_, unwanted0_.name as name0_0_ from UNWANTED unwanted0_ where unwanted0_.ID=?
    org.hibernate.MappingException: Unknown entity: java.lang.Long
    at org.hibernate.impl.SessionFactoryImpl.getEntityPer sister(SessionFactoryImpl.java:548)
    at org.hibernate.impl.SessionImpl.getEntityPersister( SessionImpl.java:1338)
    at org.hibernate.engine.ForeignKeys.isTransient(Forei gnKeys.java:180)
    at org.hibernate.event.def.AbstractSaveEventListener. getEntityState(AbstractSaveEventListener.java:487)
    at org.hibernate.event.def.DefaultPersistEventListene r.onPersist(DefaultPersistEventListener.java:70)
    at org.hibernate.event.def.DefaultPersistEventListene r.onPersist(DefaultPersistEventListener.java:38)
    at org.hibernate.impl.SessionImpl.firePersist(Session Impl.java:618)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl .java:592)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl .java:596)
    at org.apache.jsp.unwanted_jsp._jspService(unwanted_j sp.java:131)
    at org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:97)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
    at org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:332)
    at org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:314)
    at org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:264)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:174)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:117)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:108)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:151)
    at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:874)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11 ConnectionHandler.processConnection(Http11BaseProt ocol.java:665)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.process Socket(PoolTcpEndpoint.java:528)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThr ead.runIt(LeaderFollowerWorkerThread.java:81)
    at org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadPool.java:689)
    at java.lang.Thread.run(Unknown Source)

    org.apache.jasper.JasperException: Exception in JSP: /unwanted.jsp:4

    1: <%@ include file="protected.jsp" %>
    2: <%@ include file="header.jsp" %>
    3: <%
    4: Long id=Long.valueOf(request.getParameter("Id"));
    5: Session sess = HibernateUtil.currentSession();
    6: Unwanted u= new Unwanted();
    7:


    Stacktrace:
    my mapping looks like it..
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

    <hibernate-mapping>
    <class name="com.ats.custFlight.pojo.Unwanted" table="unwanted" lazy="false">
    <id column="ID" name="id" type="java.lang.Long"/>
    <property column="name" name="name" type="java.lang.String"/>
    </class>
    </hibernate-mapping>



    my code to upload data is--

    Long id=Long.valueOf(request.getParameter("Id"));
    Session sess = HibernateUtil.currentSession();
    Unwanted u= new Unwanted();

    Unwanted u2 = (Unwanted) sess.get (Unwanted.class,(long)77);
    u.setId(id);
    u.setName("RAHUL");
    try{
    sess.save(u);
    }catch( Exception e )
    {
    e.printStackTrace();
    }
    System.out.println(" size is " + u.getId());

    my hibernate.util code is--
    package com.ats.custFlight.util;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;

    import org.hibernate.*;
    import org.hibernate.cfg.*;

    public class HibernateUtil {

    private static Log log = LogFactory.getLog(HibernateUtil.class);

    private static final SessionFactory sessionFactory;

    static {
    try {
    // Create the SessionFactory
    sessionFactory = new Configuration().configure().buildSessionFactory();

    } catch (Throwable ex) {
    // Make sure you log the exception, as it might be swallowed
    log.error("Initial SessionFactory creation failed.", ex);
    ex.printStackTrace();
    throw new ExceptionInInitializerError(ex);
    }
    }

    public static final ThreadLocal < Session > session = new ThreadLocal < Session > ();

    public static Session currentSession() throws HibernateException {
    Session s = (Session) session.get();
    // Open a new Session, if this Thread has none yet
    if (s == null) {
    s = sessionFactory.openSession();
    session.set(s);
    }
    return s;
    }

    public static void closeSession() throws HibernateException {
    Session s = (Session) session.get();
    session.set(null);
    if (s != null)
    s.close();
    }
    }
    and JAVA CLASS IS..

    package com.ats.custFlight.pojo;

    public class Unwanted {

    Long id;
    String name;

    public String getName() {
    return name;
    }

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

    public Long getId() {
    return id;
    }

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

  14. #14
    rhlpnt is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default

    cud u help me...


    i am trying to insert the value in databases but it is fired by hibernate...

    Hibernate: select unwanted0_.ID as ID0_0_, unwanted0_.name as name0_0_ from UNWANTED unwanted0_ where unwanted0_.ID=?
    org.hibernate.MappingException: Unknown entity: java.lang.Long
    at org.hibernate.impl.SessionFactoryImpl.getEntityPer sister(SessionFactoryImpl.java:548)
    at org.hibernate.impl.SessionImpl.getEntityPersister( SessionImpl.java:1338)
    at org.hibernate.engine.ForeignKeys.isTransient(Forei gnKeys.java:180)
    at org.hibernate.event.def.AbstractSaveEventListener. getEntityState(AbstractSaveEventListener.java:487)
    at org.hibernate.event.def.DefaultPersistEventListene r.onPersist(DefaultPersistEventListener.java:70)
    at org.hibernate.event.def.DefaultPersistEventListene r.onPersist(DefaultPersistEventListener.java:38)
    at org.hibernate.impl.SessionImpl.firePersist(Session Impl.java:618)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl .java:592)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl .java:596)
    at org.apache.jsp.unwanted_jsp._jspService(unwanted_j sp.java:131)
    at org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:97)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
    at org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:332)
    at org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:314)
    at org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:264)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:174)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:117)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:108)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:151)
    at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:874)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11 ConnectionHandler.processConnection(Http11BaseProt ocol.java:665)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.process Socket(PoolTcpEndpoint.java:528)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThr ead.runIt(LeaderFollowerWorkerThread.java:81)
    at org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadPool.java:689)
    at java.lang.Thread.run(Unknown Source)

    org.apache.jasper.JasperException: Exception in JSP: /unwanted.jsp:4

    1: <&#37;@ include file="protected.jsp" %>
    2: <%@ include file="header.jsp" %>
    3: <%
    4: Long id=Long.valueOf(request.getParameter("Id"));
    5: Session sess = HibernateUtil.currentSession();
    6: Unwanted u= new Unwanted();
    7:


    Stacktrace:
    my mapping looks like it..
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

    <hibernate-mapping>
    <class name="com.ats.custFlight.pojo.Unwanted" table="unwanted" lazy="false">
    <id column="ID" name="id" type="java.lang.Long"/>
    <property column="name" name="name" type="java.lang.String"/>
    </class>
    </hibernate-mapping>



    my code to upload data is--

    Long id=Long.valueOf(request.getParameter("Id"));
    Session sess = HibernateUtil.currentSession();
    Unwanted u= new Unwanted();

    Unwanted u2 = (Unwanted) sess.get (Unwanted.class,(long)77);
    u.setId(id);
    u.setName("RAHUL");
    try{
    sess.save(u);
    }catch( Exception e )
    {
    e.printStackTrace();
    }
    System.out.println(" size is " + u.getId());

    my hibernate.util code is--
    package com.ats.custFlight.util;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;

    import org.hibernate.*;
    import org.hibernate.cfg.*;

    public class HibernateUtil {

    private static Log log = LogFactory.getLog(HibernateUtil.class);

    private static final SessionFactory sessionFactory;

    static {
    try {
    // Create the SessionFactory
    sessionFactory = new Configuration().configure().buildSessionFactory();

    } catch (Throwable ex) {
    // Make sure you log the exception, as it might be swallowed
    log.error("Initial SessionFactory creation failed.", ex);
    ex.printStackTrace();
    throw new ExceptionInInitializerError(ex);
    }
    }

    public static final ThreadLocal < Session > session = new ThreadLocal < Session > ();

    public static Session currentSession() throws HibernateException {
    Session s = (Session) session.get();
    // Open a new Session, if this Thread has none yet
    if (s == null) {
    s = sessionFactory.openSession();
    session.set(s);
    }
    return s;
    }

    public static void closeSession() throws HibernateException {
    Session s = (Session) session.get();
    session.set(null);
    if (s != null)
    s.close();
    }
    }
    and JAVA CLASS IS..

    package com.ats.custFlight.pojo;

    public class Unwanted {

    Long id;
    String name;

    public String getName() {
    return name;
    }

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

    public Long getId() {
    return id;
    }

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

  15. #15
    age_ues is offline Member
    Join Date
    Jul 2009
    Posts
    1
    Rep Power
    0

    Default

    ....
    org.hibernate.MappingException: Unknown entity: java.lang.Long
    ....
    ....
    Unwanted u2 = (Unwanted) sess.get (Unwanted.class,(long)77);
    ....

    i see the different type here, Long and long. please check it, may help.

Similar Threads

  1. problem with hibernate and oracle 8i
    By javadev in forum JDBC
    Replies: 4
    Last Post: 08-09-2007, 03:21 PM
  2. composite key Hibernate
    By Problem in forum Web Frameworks
    Replies: 0
    Last Post: 07-25-2007, 12:13 PM
  3. Log4J, problem with Hibernate and Spring
    By Marcus in forum Advanced Java
    Replies: 1
    Last Post: 06-06-2007, 04:22 AM
  4. composite-id and hibernate
    By Marty in forum JDBC
    Replies: 1
    Last Post: 05-28-2007, 05:34 AM

Posting Permissions

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