facebook

Primary key not created

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #335879 Reply

    IgnatzMouse
    Member

    I’m still striggling with the Hibernate Tutotial. After fixing the problem with spaces in the hmb files, I now face the next problem:
    I have a generated class from my DB with an id as primary key. I also have defined a sequence and a trigger in my DB to generate this id automatically upon insert.
    The following code however generats an exception:

    Counter counter = new Counter();
            counter.setName("Counter New");
            counter.setCountercategory(new CountercategoryDAO().findById((short) 103));
    
            // 2. Create DAO
            CounterDAO dao = new CounterDAO();
    
            // 3. Start the transaction
            Transaction tx = dao.getSession().beginTransaction();
    
            // 4. Add 
            dao.save(counter);
    
            // 5. Commit the transaction (write to database)
            tx.commit();
    
            // 6. Close the session (cleanup connections)
            dao.getSession().close();
    

    Result:

    INFO: HHH000397: Using ASTQueryTranslatorFactory
    Exception in thread "main" org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.myeclipse.hibernate.Counter
        at org.hibernate.id.Assigned.generate(Assigned.java:52)
        at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:118)
        at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204)
    

    If I “manually” set the id in my code via setId() I get this exception:

    25.02.2013 11:29:52 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
    ERROR: ORA-00001: Unique Constraint (TDK.COUNTER_PK) verletzt
    
    Exception in thread "main" org.hibernate.exception.ConstraintViolationException: ORA-00001: Unique Constraint (TDK.COUNTER_PK) verletzt
    
        at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    

    I’m sure the id I’, setting is uniq…

    So what is wrong with my class mapping (see attachment)?

    #335880 Reply

    IgnatzMouse
    Member

    looks like the attachment didn’t work, here is the file:

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <!-- 
        Mapping file autogenerated by MyEclipse Persistence Tools
    -->
    <hibernate-mapping>
        <class name="com.myeclipse.hibernate.Counter" table="COUNTER" schema="TDK">
            <id name="id" type="integer">
                <column name="ID" precision="6" scale="0" />
                <generator class="assigned" />
            </id>
            <many-to-one name="countercategory" class="com.myeclipse.hibernate.Countercategory" fetch="select">
                <column name="CATEGORY_REL" precision="4" scale="0" />
            </many-to-one>
            <property name="name" type="string">
                <column name="NAME" length="40" not-null="true" />
            </property>
            <set name="counterevents" inverse="true">
                <key>
                    <column name="COUNTER_REL" precision="6" scale="0" />
                </key>
                <one-to-many class="com.myeclipse.hibernate.Counterevent" />
            </set>
        </class>
    </hibernate-mapping>
    
    #335932 Reply

    IgnatzMouse,

    I suggest you cross post to Hibernate forums or development forums like stackoverflow.com for better support as this is a development query.

Viewing 3 posts - 1 through 3 (of 3 total)
Reply To: Primary key not created

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