java - 无法将数据插入到数据库 mysql 中的我的表中

标签 java mysql hibernate

我在插入数据库时​​遇到问题。这是我插入数据库的设置。

首先,我有 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;
        }



}

这是我的用于插入数据库的类

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

最诚挚的问候, 福阿德

最佳答案

我建议您启用 MySQL 日志,然后按照 https://dev.mysql.com/doc/refman/5.7/en/server-logs.html 中的说明进行操作。并且一定要刷新日志。日志应该位于/bar/log/MySQL

关于java - 无法将数据插入到数据库 mysql 中的我的表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61079682/

相关文章:

java - 打开文档时将图像的 url 转换为实际图像

java - 为什么在这个例子中使用 Bean——它看起来像是毫无意义的间接?

php - 跨多个表维护唯一标识

java - 如何过滤 Hibernate 搜索结果以仅包含排序期间具有特殊字段值的对象之后的对象?

java - 如何解决javax.persistence.EntityExistsException : a different object with the same identifier value was already associated with the session

java - Locale对象可以用hibernate存储在数据库中吗

java - 资源上的 Spring Data Rest 自定义链接

java - 使用java中的套接字制作简单的客户端/代理/服务器程序

mysql - 如何设计索引/查询以避免简单 MySQL 查询中的文件排序?

mysql - 将多个表中的数据插入到一个表中