繁体   English   中英

无法在数据库 mysql 中向我的表中插入数据

[英]cannot insert data to my table in database mysql

插入我的数据库时出现问题。 这是我插入数据库的设置。

首先,我有 getExecutionReport 的方法

首先我导入什么

import com.dxtr.hibernate.DAOInsert;
import com.dxtr.hibernate.newOrderSingleEntity;
    private void getExecutionReport(quickfix.Message message, String tipeMessage) {
                // TODO Auto-generated method stub
                 try {
                     System.out.print("Message "+ message);
                    System.out.print("getExecutionReport "+tipeMessage);
                    newOrderSingleEntity newordersingleObj = new newOrderSingleEntity();
                        newordersingleObj.setSymbol(message.getString(Symbol.FIELD));
                        System.out.print(" symbol "+message.getString(Symbol.FIELD));
                        newordersingleObj.setMsgType(tipeMessage);
                        System.out.print(" tipeMessage "+tipeMessage);
                        newordersingleObj.setBodyLength(216);
                        System.out.print(" Body Length "+216);
                        newordersingleObj.setFixProtocol("FIX.4.2");
                        System.out.print("FixProtocol +FIX.4.2");
                        newordersingleObj.setTransactTime(message.getString(TransactTime.FIELD));
                        System.out.print(" time " +message.getString(TransactTime.FIELD));
                        newordersingleObj.setClOrdID(message.getString(ClOrdID.FIELD));
                        System.out.print(" ClOrdID "+message.getString(ClOrdID.FIELD));
                        newordersingleObj.setOrderID(message.getString(OrderID.FIELD));
                        System.out.print(" OrderID "+message.getString(OrderID.FIELD));
                        newordersingleObj.setExecID(message.getString(ExecID.FIELD));
                        System.out.print(" ExecID "+message.getString(ExecID.FIELD));
                        newordersingleObj.setExecTransType(message.getString(ExecTransType.FIELD));
                        System.out.print(" ExecTransType "+message.getString(ExecTransType.FIELD));
                        newordersingleObj.setOrdType(message.getString(OrdType.FIELD));
                        System.out.print(" OrdType "+message.getString(OrdType.FIELD));
                        newordersingleObj.setExecType(message.getString(ExecType.FIELD));
                        System.out.print(" ExecType "+message.getString(ExecType.FIELD));
                        newordersingleObj.setOrdStatus(message.getString(OrdStatus.FIELD));
                        System.out.print(" OrdStatus "+message.getString(OrdStatus.FIELD));
                        newordersingleObj.setSide(message.getInt(Side.FIELD));
                        System.out.print(" Side "+message.getInt(Side.FIELD));
                        newordersingleObj.setOrderQty(message.getDouble(OrderQty.FIELD));
                        System.out.print(" OrderQty "+message.getDouble(OrderQty.FIELD));
                        newordersingleObj.setLeavesQty(message.getDouble(LeavesQty.FIELD));
                        System.out.print(" LeavesQty "+message.getDouble(LeavesQty.FIELD));
                        newordersingleObj.setCumQty(message.getDouble(CumQty.FIELD));
                        System.out.print(" CumQty "+message.getDouble(CumQty.FIELD));
                        newordersingleObj.setLastShares(message.getInt(LastShares.FIELD));
                        System.out.print(" LastQty "+message.getInt(LastShares.FIELD));

                        System.out.print("sebelum insert ke db");
                     DAOInsert.newOrderSingleInsert(newordersingleObj);

                } catch (FieldNotFound e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }

这段代码没问题。 但是当我想插入我的数据库时,我的数据无法插入它。

这是我的实体

package com.dxtr.hibernate;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="New_Single_Order_Response",schema="dxtr_trades")
public class newOrderSingleEntity  implements Serializable{
     private static final long serialVersionUID = -1234424538928L;
      @Id
        @Column(name="response_id")
        @GeneratedValue(strategy=GenerationType.AUTO)
        private Integer id;

        @Column(name="Fix_Protocol")
        private String FixProtocol;

        @Column(name="BodyLength")
        private Integer BodyLength;

        @Column(name="MsgSeqNum")
        private Integer MsgSeqNum;

        @Column(name="MsgType")
        private String MsgType;

        @Column(name="SenderCompId")
        private String SenderCompId;

        @Column(name="SendingTime")
        private String SendingTime;

        @Column(name="TargetCompID")
        private String TargetCompID;

        @Column(name="AveragePrice")
        private Double AveragePrice;

        @Column(name="ClOrdID")
        private String ClOrdID;

        @Column(name="CumQty")
        private double CumQty;

        @Column(name="Currency")
        private String Currency;

        @Column(name="ExecID")
        private String ExecID;

        @Column(name="LastShares")
        private double LastShares;

        @Column(name="OrderQty")
        private Double OrderQty;

        @Column(name="OrdStatus")
        private String OrdStatus;

        @Column(name="OrdType")
        private String OrdType;

        @Column(name="Side")
        private Integer Side;

        @Column(name="Symbol")
        private String Symbol;

        @Column(name="TransactTime")
        private String TransactTime;

        @Column(name="ExecType")
        private String ExecType;

        @Column(name="LeavesQty")
        private double LeavesQty;

        @Column(name="CheckSum")
        private Integer CheckSum;

        @Column(name="ExecTransType")
        private String ExecTransType;

        @Column(name="OrderID")
        private String OrderID;


        public Integer getResponse_ID() {
            return this.id;
        }

        public void setResponse_ID(Integer id) {
            this.id = id;
        }

        public String getFixProtocol() {
            return this.FixProtocol;
        }

        public void setFixProtocol(String FixProtocol) {
            this.FixProtocol = FixProtocol;
        }

        public Integer getBodyLength() {
            return this.BodyLength;
        }

        public void setBodyLength(Integer BodyLength) {
            this.BodyLength = BodyLength;
        }

        public Integer getMsgSeqNum() {
            return this.MsgSeqNum;
        }

        public void setMsgSeqNum(Integer MsgSeqNum) {
            this.MsgSeqNum = MsgSeqNum;
        }


        public String getMsgType() {
            return this.MsgType;
        }

        public void setMsgType(String MsgType) {
            this.MsgType = MsgType;
        }

        public String getSenderCompId() {
            return this.SenderCompId;
        }

        public void setSenderCompId(String SenderCompId) {
            this.SenderCompId = SenderCompId;
        }

        public String getSendingTime() {
            return this.SendingTime;
        }

        public void setBodyLength(String SendingTime) {
            this.SendingTime = SendingTime;
        }

        public String getTargetCompID() {
            return this.TargetCompID;
        }

        public void setTargetCompID(String TargetCompID) {
            this.TargetCompID = TargetCompID;
        }

        public Double getAveragePrice() {
            return this.AveragePrice;
        }

        public void setAveragePrice(Double AveragePrice) {
            this.AveragePrice = AveragePrice;
        }

        public String getClOrdID() {
            return this.ClOrdID;
        }

        public void setClOrdID(String ClOrdID) {
            this.ClOrdID = ClOrdID;
        }

        public double getCumQty() {
            return this.CumQty;
        }

        public void setCumQty(double CumQty) {
            this.CumQty = CumQty;
        }

        public String getCurrency() {
            return this.Currency;
        }

        public void setCurrency(String Currency) {
            this.Currency = Currency;
        }

        public String getExecID() {
            return this.ExecID;
        }

        public void setExecID(String ExecID) {
            this.ExecID = ExecID;
        }

        public double getLastShares() {
            return this.LastShares;
        }

        public void setLastShares(double LastShares) {
            this.LastShares = LastShares;
        }

        public Double getOrderQty() {
            return this.OrderQty;
        }

        public void setOrderQty(Double OrderQty) {
            this.OrderQty = OrderQty;
        }

        public String getExecType() {
            return this.ExecType;
        }

        public void setExecType(String ExecType) {
            this.ExecType = ExecType;
        }

        public String getOrdStatus() {
            return this.OrdStatus;
        }

        public void setOrdStatus(String OrdStatus) {
            this.OrdStatus = OrdStatus;
        }

        public String getOrdType() {
            return this.OrdType;
        }

        public void setOrdType(String OrdType) {
            this.OrdType = OrdType;
        }

        public Integer getSide() {
            return this.Side;
        }

        public void setSide(Integer Side) {
            this.Side = Side;
        }

        public String getSymbol() {
            return this.Symbol;
        }

        public void setSymbol(String Symbol) {
            this.Symbol = Symbol;
        }

        public String getTransactTime() {
            return this.TransactTime;
        }

        public void setTransactTime(String TransactTime) {
            this.TransactTime = TransactTime;
        }

        public double getLeavesQty() {
            return this.LeavesQty;
        }

        public void setLeavesQty(double LeavesQty) {
            this.LeavesQty = LeavesQty;
        }

        public Integer getCheckSum() {
            return this.CheckSum;
        }

        public void setCheckSum(Integer CheckSum) {
            this.CheckSum = CheckSum;
        }

        public String getExecTransType() {
            return this.ExecTransType;
        }

        public void setExecTransType(String ExecTransType) {
            this.ExecTransType = ExecTransType;
        }

        public String getOrderID() {
            return this.OrderID;
        }

        public void setOrderID(String OrderID) {
            this.OrderID = OrderID;
        }

        public String toString() {
            return "OrderDetail?= Id: " + this.id + ", ClOrdID: " + this.ClOrdID + ", ExecID No.: " + this.ExecID + ", Symbol: " + this.Symbol;
        }



}

在这里我的 class 用于插入数据库

package com.dxtr.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import quickfix.StringField;

public class DAOInsert {
    private DAOInsert dAOInsert;
    static Session sessionObj;
    static SessionFactory sessionFactoryObj;

    public final static Logger logger = Logger.getLogger(DAOInsert.class);

    // This Method Is Used To Create The Hibernate's SessionFactory Object
    public static SessionFactory buildSessionFactory() {
        // Creating Configuration Instance & Passing Hibernate Configuration File
        Configuration configObj = new Configuration();
        configObj.configure("hibernate.cfg.xml");

        // Since Hibernate Version 4.x, ServiceRegistry Is Being Used
        ServiceRegistry serviceRegistryObj = new StandardServiceRegistryBuilder().applySettings(configObj.getProperties()).build(); 

        // Creating Hibernate SessionFactory Instance
        sessionFactoryObj = configObj.buildSessionFactory(serviceRegistryObj);
        return sessionFactoryObj;
    }

    public static void newOrderSingleInsert(newOrderSingleEntity newordersingleObj){
          int count = 0;
          try {
              System.out.print("sudah di DAOInsert");
              // Getting Session Object From SessionFactory
              sessionObj = buildSessionFactory().openSession();
              // Getting Transaction Object From Session Object
              sessionObj.beginTransaction();
              sessionObj.persist(newordersingleObj);
              System.out.print(sessionObj.contains(newordersingleObj));
              // Creating Transaction Entities
            //  newordersingleObj = new NewOrderSingle();             
              //newordersingleObj.setFixProtocol("FIX.4.2");
              //newordersingleObj.setBodyLength(250);
                  //sessionObj.save(newordersingleObj);

               //Committing The Transactions To The Database
              sessionObj.getTransaction().commit();
              //logger.info("\nSuccessfully Created '" + count + "' Records In The Database!\n");
          } catch(Exception e) {
              if(null != sessionObj.getTransaction()) {
                  logger.info("\n.......Transaction Is Being Rolled Back.......\n");
                  sessionObj.getTransaction().rollback();
              }
              e.printStackTrace();
          } finally {
              if(sessionObj != null) {
                  sessionObj.close();
              }
          }
      }



}

我还设置了persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="FastMatch" transaction-type="RESOURCE_LOCAL">
        <class>com.dxtr.hibernate.newOrderSingleEntity</class>
        <properties>
            <property name="eclipselink.logging.level" value="INFO"/>
             <property name="eclipselink.logging.level.sql" value="FINE"/>
            <property name="eclipselink.logging.parameters" value="true"/> 
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://trades/tradesnew />
            <property name="javax.persistence.jdbc.user" value="user" />
            <property name="javax.persistence.jdbc.password" value="password" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="true" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="eclipselink.logging.level.connection" value="FINEST"/>
        </properties>
    </persistence-unit>
</persistence>

我也有 hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//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://url/trades</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.username">user</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <mapping class="com.dxtr.hibernate.newOrderSingleEntity"></mapping>
    </session-factory>
</hibernate-configuration>

但是当我想插入我的数据库时,这个数据无法插入它..但我不知道错误在哪里......我已经使用

catch(Exception e) {
              if(null != sessionObj.getTransaction()) {
                  logger.info("\n.......Transaction Is Being Rolled Back.......\n");
                  sessionObj.getTransaction().rollback();
              }
              e.printStackTrace();
          } finally {
              if(sessionObj != null) {
                  sessionObj.close();
              }

但是我的控制台打印中没有错误...那么我的代码的问题在哪里? 有关信息,我使用 mysql5.7 版本并使用 aws

最好的问候,福阿德

I would suggest you enable MySQL logs, then follow the instructions at https://dev.mysql.com/doc/refman/5.7/en/server-logs.html and be sure to flush the logs. 日志应该在 /bar/log/MySQL

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM