1

My add code works for every object expect the new table I just added (Report). I can't seem to find the problem and i just get this error:

Could not execute statement when using .save(object)

Report.hbm.xml

<hibernate-mapping>
<class name="com.atp.Models.Report" table="report">
    <meta attribute="class-description">
        This class contains the report details.
    </meta>
    <id name="environmentName" type="string" column="environmentName">
        <generator class="native"/>
    </id>
    <property name="individual" column="Individual" type="int"/>
    <property name="corporation" column="Corporation" type="int"/>
    <property name="dda" column="DDA" type="int"/>
    <property name="sav" column="SAV" type="int"/>
    <property name="mtg" column="MTG" type="int"/>
    <property name="sepaOut" column="SEPAOUT" type="int"/>
    <property name="sepaIn" column="SEPAIN" type="int"/>
    <property name="atm" column="ATM" type="int"/>
</class>

MySqL Table

My Sql Table

Java Add to database (all the fields are right)

    //Create new object
    Report report = new Report();
    report.setEnvironmentName(environmentName);
    report.setIndividual(individual);
    report.setCorporation(corporation);
    report.setDda(dda);
    report.setSav(sav);
    report.setMtg(mtg);
    report.setSepaIn(sepaIn);
    report.setSepaOut(sepaOut);
    report.setAtm(atm);

    ManageCreation.AddToDatabase(report);

public static void AddToDatabase(Object object) {
    SessionFactory factory = HibernateUtil.GetSessionFactory();
    Session session = factory.openSession();
    Transaction tx = null;
    try{
        tx = session.beginTransaction();
        session.save(object);
        tx.commit();
    }catch (HibernateException e) {
        if (tx!=null) tx.rollback();
        e.printStackTrace();
    }finally {
        session.close();
    }
}
vmonteco
  • 14,136
  • 15
  • 55
  • 86
  • 2
    The full stack trace of your exception could be of some use here. Also, the exact line where the exception occurs (.save() or .commit() ?) – Marvin Dec 05 '17 at 10:53
  • the VARCHAR(45) primary key is making my skin crawl – Zeromus Dec 05 '17 at 10:54
  • @Zeromus I'm a beginner. Give advice please. –  Dec 05 '17 at 11:00
  • Does Report entity implement Serializable interface? – gkatiforis Dec 05 '17 at 11:00
  • @Tiago Silvia sometimes it's actually correct to use a natural key over a surrogate key but you have to be sure that that field value will never change... https://stackoverflow.com/questions/332300/is-there-a-real-performance-difference-between-int-and-varchar-primary-keys – Zeromus Dec 05 '17 at 11:06
  • A [mcve] should include a stacktrace... – AxelH Dec 05 '17 at 11:07
  • The native generator (for a _surrogate_ key) looks suspicious for a string key, you yourself fill (a _natural_ key). – Joop Eggen Dec 05 '17 at 11:14

1 Answers1

0

Without full stacktrace i can only guess that the xml configuration on the varchar pk (that you actually set and is not autogenerated) is somehow causing the problem... try to remove that

If you set your pk field in the code

Report report = new Report();
report.setEnvironmentName(environmentName);

you have no need for an autogeneration configuration

<generator class="native"/>

also i think that hibernate is expecting a number-type of value to autoincrement and this may be actually messing up things

Zeromus
  • 4,472
  • 8
  • 32
  • 40
  • Why you say I shouldn't use string as PK? –  Dec 05 '17 at 11:01
  • I just want to later extract the data from the report table where environmentName = "something" –  Dec 05 '17 at 11:02
  • no in the answer i just point out that the xml configuration you provided is telling hibernate to use native autogeneration, but on string pk that doesnt make much sense... moreover you actually do set it in the code report.setEnvironmentName(environmentName); – Zeromus Dec 05 '17 at 11:03
  • @TiagoSilva use some id like environmentId of type Long etc as primary key rather than a string value such as environment name. auto generate the primary key – TruckDriver Dec 05 '17 at 11:06