Hibernate performace - to many select queries in console log

时间:2016-02-12 21:24:23

标签: java spring hibernate

I think that I have problem with hibernate performance. Maybe I am doing something wrong. I am using Spring 4 and Hibernate 4. I have two tables in database, Driver and Fleet connected ManyToMany. Table DRIVER_FLEET connects them bout and consist of driverID and fleetID.

Here is the example, from class Driver, that represents how my connection looks like.

import random
import os

class Entry:
    def __init__(self, deutsch, englisch):
        self.deutsch = deutsch
        self.englisch = englisch

    def toString(self):
        return self.deutsch + " - " + self.englisch

eintraege = [Entry("hallo", "hello")]   **<--Wanna save this list**
directory = r'C:\Users\Peter\desktop'

def eingabe():   #means 'input' in english
    while True:
        deutsch = input("Deutsches Wort: ")
        if deutsch == '#exit#':
            return
        englisch = input("Englisches Wort: ")
        if englisch == '#exit#':
            return
        eintraege.append(Entry(deutsch, englisch))
        w = open('dictionary.txt', 'a')
        w.write(' - '.join(eintraege))  **<---------Problem here; tried multiple solutions**

def abfrage():  #means 'query' in english
    while True:
        i = random.randint(0, len(eintraege) - 1)
        englisch = input("Englische Übersetzung von " + eintraege[i].deutsch + ": ")
        if englisch == '#exit#':
            return
        if eintraege[i].englisch == englisch:
            print("Korrekt!")
        else:
            print("Leider falsch. Richtig wäre:", eintraege[i].englisch)

def printall():
    for eintrag in eintraege:
        print(eintrag.toString)

os.chdir(directory)        
r = open('dictionary.txt', 'r')
r.read()

while True:
    print("Befehle:\n\
1.) eingabe: Ermöglicht die Bearbeitung des Wörterbuchs\n\
2.) abfrage: Zufällige Abfrage der Vokabel\n\
3.) ausgabe: Zeigt alle eingegebenen Vokabel an\n\
4.) beenden: Beendet das Programm\n\n")

    befehl = input("Befehl: ")
    if befehl == 'eingabe':
        eingabe()
    elif befehl == 'abfrage':
        abfrage()
    elif befehl == 'beenden':
        break
    elif befehl == 'ausgabe':  #means 'output' in english
        printall()
    else:
        print("Kein bekannter Befehl!")

Here is class Fleet:

@ManyToMany(fetch = FetchType.LAZY, cascade=CascadeType.PERSIST)  
    @JoinTable(name="DRIVER_FLEET", 
                        joinColumns= { @JoinColumn(name="driverID")}, 
                        inverseJoinColumns={@JoinColumn(name="fleetID")}) 
        private List<Fleet> fleet = new ArrayList<Fleet>(); 
    public List<Fleet> getFleet() {
        return fleet;
    }
    public void setFleet(List<Fleet> fleet) {
        this.fleet = fleet;
    }

Now when I save Driver and Fleet into database using hibernate save(), I have this result in console:

@ManyToMany(mappedBy = "Driver")
    public Set<Driver> getDriver() {
        return getDriver();
    }

From this console log you can see that I have two many these rows

Hibernate: select driver0_.driverID as driverID1_2_, driver0_.commision as commisio2_2_, driver0_.dateOfBirth as dateOfBi3_2_, driver0_.driverDriveRateAmount as driverDr4_2_, driver0_.driverDriveRateCurrency as driverDr5_2_, driver0_.driverNumber as driverNu6_2_, driver0_.driverPayRateAmount as driverPa7_2_, driver0_.driverPayRateCurrency as driverPa8_2_, driver0_.driverPerOrderAmount as driverPe9_2_, driver0_.driverPerOrderCurrency as driverP10_2_, driver0_.firstName as firstNa11_2_, driver0_.gender as gender12_2_, driver0_.lastName as lastNam13_2_, driver0_.middleInitials as middleI14_2_ from Driver driver0_
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: select driver0_.driverID as driverID1_2_, driver0_.commision as commisio2_2_, driver0_.dateOfBirth as dateOfBi3_2_, driver0_.driverDriveRateAmount as driverDr4_2_, driver0_.driverDriveRateCurrency as driverDr5_2_, driver0_.driverNumber as driverNu6_2_, driver0_.driverPayRateAmount as driverPa7_2_, driver0_.driverPayRateCurrency as driverPa8_2_, driver0_.driverPerOrderAmount as driverPe9_2_, driver0_.driverPerOrderCurrency as driverP10_2_, driver0_.firstName as firstNa11_2_, driver0_.gender as gender12_2_, driver0_.lastName as lastNam13_2_, driver0_.middleInitials as middleI14_2_ from Driver driver0_
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: insert into Driver (commision, dateOfBirth, driverDriveRateAmount, driverDriveRateCurrency, driverNumber, driverPayRateAmount, driverPayRateCurrency, driverPerOrderAmount, driverPerOrderCurrency, firstName, gender, lastName, middleInitials) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into DRIVER_LICENSE (expirationDate, stateIssued, driverID, licenseID) values (?, ?, ?, ?)
Hibernate: insert into DRIVER_FLEET (driverID, fleetID) values (?, ?)

The problem is, if I have more rows in table DRIVER_FLEET, then I have more rows of those select queries. Is there some possibility that I have made something wrong in ManyToMany connection? Can my performance be better?

1 个答案:

答案 0 :(得分:0)

我们等待@Milan更新他的问题,因为@JBNizet指出了一些严重的问题,让我们尝试远射回答:

仅仅因为您在日志中发现了多条重复的行,并不意味着事件不止一次发生。在某些情况下,您的日志记录框架可能会记录单个事件的多条消息。

例如,当您在类包的不同深度上设置多个记录器时,如下所示:

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>

  <logger name="chapters">
    <appender-ref ref="STDOUT" />
  </logger>

  <logger name="chapters.configuration">
    <appender-ref ref="STDOUT" />
  </logger>

  <logger name="chapters.configuration.Foo">
    <appender-ref ref="STDOUT" />
  </logger>

  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

您可以使用Logback在控制台中获取4x事件,因为默认情况下记录是累积的,请参阅additivity="false"进行修复。