0

今、私は理解できない本当に奇妙な問題を抱えています。

休止状態を適切に構成しました。mysql データベースから問題なくデータをロードできます。しかし、データを挿入できません。ここに私の hibernate.cfg.xml があります:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bookmaker</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="javax.persistence.validation.mode">none</property>

<mapping class="de.wettprofi.objects.Bookmaker"/>
<mapping class="de.wettprofi.objects.Match"/>
<mapping class="de.wettprofi.objects.LogEntry"/>

</session-factory>
</hibernate-configuration>

クラスLogEntryについてです。オブジェクトをデータベースに永続化する必要があるコードを次に示します。

public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
    {
        BufferedReader reader = request.getReader();
        String json = reader.readLine();

        try {
            LogEntry log_entry = null;

            if (json != null && !json.isEmpty()) {
                log_entry = new LogEntry();

                JSONParser parser = new JSONParser();
                JSONObject json_dict = (JSONObject) parser.parse( json );

                //initalize proper LogEntry object
            } 
            if( log_entry != null ) {
                Session session = get_hibernate_session();
                System.out.println("Logging of LogEntry into the database\n" + log_entry.toString() );
                session.save( log_entry );
                session.flush();
                System.out.println("Logging DONE");
            }

        } catch( Exception e ) {
            e.printStackTrace();
        }
    }



    public Session get_hibernate_session()
    {
        //Opening a hibernate session
        SessionFactory sessionFactory = null;
        try {
            Configuration hibConfiguration = new Configuration().addResource("hibernate.cfg.xml").configure();       
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(hibConfiguration.getProperties()).buildServiceRegistry();
            sessionFactory = hibConfiguration.buildSessionFactory(serviceRegistry);
        } catch( Exception e ) {
            e.printStackTrace();
        }

        return sessionFactory.withOptions().openSession();
    }

LogEntry クラス オブジェクトは次のようになります (ゲッターとセッターは省略されています)。

@Entity
@Table( name = "LogEntries" )
public class LogEntry {

    public static int MESSAGE = 1;
    public static int BOOKMAKER = 2;
    public static int CLICKLOG = 3;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column( name = "lid" )
    public int lid;

    @Column( name = "id" )
    public String id;

    @Column( name = "date" )
    public Date date;

    @Column( name = "type" )
    public int type;

    @Column( name = "message" )
    public String message;

    @Column( name = "bookmaker" )
    public String bookmaker;

    @Column( name = "match_id" )
    public int match_id;

    @Column( name = "result" )
    public String result;


    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append("      lid = " + lid  + "\n");
        sb.append("       id = " + id  + "\n");
        sb.append("     date = " + date.toString()  + "\n");
        sb.append("     type = " + type  + "\n");
        sb.append("  message = " + message  + "\n");
        sb.append("bookmaker = " + bookmaker  + "\n");
        sb.append(" match_id = " + match_id  + "\n");
        sb.append("   result = " + result  + "\n");
        return sb.toString();
    }

したがって、Tomcat インスタンスを実行して何が起こったのかを調べると、次の出力が得られます。

INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Apr 24, 2013 9:55:18 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Apr 24, 2013 9:55:18 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Logging of LogEntry into the database
      lid = 0
       id = 50569803719166097161
     date = Mon Apr 22 21:01:53 CEST 2013
     type = 1
  message = MainVC
bookmaker = null
 match_id = 0
   result = null

Hibernate: insert into LogEntries (bookmaker, date, id, match_id, message, result, type) values (?, ?, ?, ?, ?, ?, ?)
Logging DONE

ただし、この後、データベースを調べると、テーブルに何も書き込まれていないことがわかります。

これは私のテーブルスキーマです:

mysql> describe LogEntries;
+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| lid       | int(11)      | NO   | PRI | NULL    | auto_increment |
| bookmaker | varchar(255) | YES  |     | NULL    |                |
| date      | datetime     | YES  |     | NULL    |                |
| id        | varchar(255) | YES  |     | NULL    |                |
| match_id  | int(11)      | YES  |     | NULL    |                |
| message   | varchar(255) | YES  |     | NULL    |                |
| result    | varchar(255) | YES  |     | NULL    |                |
| type      | int(11)      | YES  |     | NULL    |                |
+-----------+--------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)

この問題の原因が何か考えられる人はいますか? 私は無知です:(

4

2 に答える 2