草庐IT

关于 java:Table not getting created using Hibernate Automatic Query Generation

codeneng 2023-03-28 原文

Table not getting created using Hibernate Automatic Query Generation

低于错误

1
2
3
4
5
6
7
8
9
10
11
12
13
Caused by: org.apache.derby.client.am.SqlException: Table/View 'SO_ITEM_DTLS' does not exist.
    at org.apache.derby.client.am.Statement.completeSqlca(Unknown Source)
    at org.apache.derby.client.net.NetStatementReply.parsePrepareError(Unknown Source)
    at org.apache.derby.client.net.NetStatementReply.parsePRPSQLSTTreply(Unknown Source)
    at org.apache.derby.client.net.NetStatementReply.readPrepareDescribeOutput(Unknown Source)
    at org.apache.derby.client.net.StatementReply.readPrepareDescribeOutput(Unknown Source)
    at org.apache.derby.client.net.NetStatement.readPrepareDescribeOutput_(Unknown Source)
    at org.apache.derby.client.am.Statement.readPrepareDescribeOutput(Unknown Source)
    at org.apache.derby.client.am.PreparedStatement.readPrepareDescribeInputOutput(Unknown Source)
    at org.apache.derby.client.am.PreparedStatement.flowPrepareDescribeInputOutput(Unknown Source)
    at org.apache.derby.client.am.PreparedStatement.prepare(Unknown Source)
    at org.apache.derby.client.am.Connection.prepareStatementX(Unknown Source)
    ... 100 more

我正在使用 derby 数据库和 hibernate 来自动生成模式。
但是在为所有实体生成 SQL 时,表并没有被创建到数据库中。代码在两个实体类之间具有@OneToMany 关系,并且父表也包含 EmbeddedID。 Hibernate 配置文件和实体类如下。

hibernate.cfg.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
       "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
       "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
        <property name="hibernate.connection.url">jdbc:derby://localhost:1527/mcodb;create=true;user=mco;password=mco</property>
        <property name="hibernate.connection.username">mco</property>
        <property name="hibernate.connection.password">mco</property>
        <property name="hibernate.hbm2ddl.auto">create</property>
        <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
        <property name="show_sql">true</property>
        <mapping class="mco.com.billing.app.model.AdminReportingPanel" />
        <mapping class="mco.com.billing.app.model.SODetails" />
        <mapping class="mco.com.billing.app.model.SOItemDetails" />
        <mapping class="mco.com.billing.app.model.BillCategories" />
        <mapping class="mco.com.billing.app.model.BillHeads" />
        <mapping class="mco.com.billing.app.model.Dealers" />
        <mapping class="mco.com.billing.app.model.FinancialYears" />
    </session-factory>
</hibernate-configuration>

SODetails 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package mco.com.billing.app.model;

import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="SO_DTLS")
public class SODetails {


    @EmbeddedId
    SODetailsEmbeddable soDetailsEmbeddable;

    @Column(name ="LPR_NO", nullable = false, length = 400)
    String lprNo;

    @Column(name ="DEALER_NAME", nullable = false, length = 200)
    String dealerName;

    @Column(name ="SO_DATE")
    Date soDate;

    @Column(name ="VAT", length = 200)
    String vat;

    @Column(name="NO_OF_QUOTATIONS", nullable = false, length=100)
    String noOfQuotations;

    @Column(name="LINKED_SO", nullable = false, length=100)
    String linkedSO;

    @Column(name="BILL_HEAD", nullable = false, length=100)
    String billHead;

    @Column(name="BILL_CATEGORY", nullable = false, length=100)
    String billCategory;

    @Column(name="NO_OF_CSTS", nullable = false, length=100)
    String noOfCSTs;

    @Column(name="NO_OF_CRVS", nullable = false, length=100)
    String noOFCRVs;

    @Column(name ="SO_GRAND_TOTAL_AMOUNT", nullable = false, length = 100)
    String sOGrandTotalAmount;

    @Column(name ="SO_GRAND_TOTAL_ROUND_OFF_AMOUNT", nullable = false, length = 100)
    String sOGrandTotalRoundOfAmount;

    @Column(name ="BILL_GRAND_TOTAL_AMOUNT", length = 100)
    String billGrandTotalAmount;

    @Column(name ="BILL_GRAND_TOTAL_ROUND_OFF_AMOUNT", length = 100)
    String billGrandTotalRoundOffAmount;

    @Column(name="IS_BILL_GENERATED", length = 100)
    boolean isBillGenerated;

    @Column(name="IS_SHORT_CLOSED_SO", length = 100)
    boolean isShortClosedSO;

    @Column(name="IS_SHORT_CLOSED_GENERATED", length = 100)
    boolean isShortClosedGenerated;

    @Column(name="IS_LD_ATTACHED", length = 100)
    boolean isLDAttached;

    @Column(name="LD_AMOUNT", length = 100)
    String lDAmount;

    @Column(name="ITEM_DUE_DATE", length = 100)
    Date itemDueDate;

    @Column(name="NO_OF_DELAY_WEEKS", length = 100)
    String noOfWeeksDelay;

    @Column(name="LD_PERCENTAGE", length = 100)
    String ldPercentage;

    @Column(name="FINAL_AMOUNT_AFTER_LD", length = 100)
    String finalAmountAfterLD;

    @Column(name="AMOUNT_ON_WHICH_LD_CALCULATED", length = 100)
    String amountOnWhichLDCalculated;

    public String getAmountOnWhichLDCalculated() {
        return amountOnWhichLDCalculated;
    }
    public void setAmountOnWhichLDCalculated(String amountOnWhichLDCalculated) {
        this.amountOnWhichLDCalculated = amountOnWhichLDCalculated;
    }
    public String getLdPercentage() {
        return ldPercentage;
    }
    public void setLdPercentage(String ldPercentage) {
        this.ldPercentage = ldPercentage;
    }
    public String getFinalAmountAfterLD() {
        return finalAmountAfterLD;
    }
    public void setFinalAmountAfterLD(String finalAmountAfterLD) {
        this.finalAmountAfterLD = finalAmountAfterLD;
    }
    public Date getItemDueDate() {
        return itemDueDate;
    }
    public void setItemDueDate(Date itemDueDate) {
        this.itemDueDate = itemDueDate;
    }
    public String getNoOfWeeksDelay() {
        return noOfWeeksDelay;
    }
    public void setNoOfWeeksDelay(String noOfWeeksDelay) {
        this.noOfWeeksDelay = noOfWeeksDelay;
    }
    @OneToMany(fetch=FetchType.LAZY, mappedBy="sODetails", cascade = CascadeType.ALL)
    Set<SOItemDetails> setSOItemDetails;

    public String getVat() {
        return vat;
    }
    public void setVat(String vat) {
        this.vat = vat;
    }
    public String getNoOfQuotations() {
        return noOfQuotations;
    }
    public void setNoOfQuotations(String noOfQuotations) {
        this.noOfQuotations = noOfQuotations;
    }
    public String getLinkedSO() {
        return linkedSO;
    }
    public void setLinkedSO(String linkedSO) {
        this.linkedSO = linkedSO;
    }
    public String getBillHead() {
        return billHead;
    }
    public void setBillHead(String billHead) {
        this.billHead = billHead;
    }
    public String getBillCategory() {
        return billCategory;
    }
    public void setBillCategory(String billCategory) {
        this.billCategory = billCategory;
    }

    public String getNoOfCSTs() {
        return noOfCSTs;
    }
    public void setNoOfCSTs(String noOfCSTs) {
        this.noOfCSTs = noOfCSTs;
    }
    public String getNoOFCRVs() {
        return noOFCRVs;
    }
    public void setNoOFCRVs(String noOFCRVs) {
        this.noOFCRVs = noOFCRVs;
    }
    public boolean isShortClosedSO() {
        return isShortClosedSO;
    }
    public void setShortClosedSO(boolean isShortClosedSO) {
        this.isShortClosedSO = isShortClosedSO;
    }
    public boolean isShortClosedGenerated() {
        return isShortClosedGenerated;
    }
    public void setShortClosedGenerated(boolean isShortClosedGenerated) {
        this.isShortClosedGenerated = isShortClosedGenerated;
    }
    public String getlDAmount() {
        return lDAmount;
    }
    public void setlDAmount(String lDAmount) {
        this.lDAmount = lDAmount;
    }
    public String getLprNo() {
        return lprNo;
    }
    public void setLprNo(String lprNo) {
        this.lprNo = lprNo;
    }

    public String getsOGrandTotalAmount() {
        return sOGrandTotalAmount;
    }
    public void setsOGrandTotalAmount(String sOGrandTotalAmount) {
        this.sOGrandTotalAmount = sOGrandTotalAmount;
    }
    public String getsOGrandTotalRoundOfAmount() {
        return sOGrandTotalRoundOfAmount;
    }
    public void setsOGrandTotalRoundOfAmount(String sOGrandTotalRoundOfAmount) {
        this.sOGrandTotalRoundOfAmount = sOGrandTotalRoundOfAmount;
    }
    public String getBillGrandTotalAmount() {
        return billGrandTotalAmount;
    }
    public void setBillGrandTotalAmount(String billGrandTotalAmount) {
        this.billGrandTotalAmount = billGrandTotalAmount;
    }
    public String getBillGrandTotalRoundOffAmount() {
        return billGrandTotalRoundOffAmount;
    }
    public void setBillGrandTotalRoundOffAmount(String billGrandTotalRoundOffAmount) {
        this.billGrandTotalRoundOffAmount = billGrandTotalRoundOffAmount;
    }
    public String getDealerName() {
        return dealerName;
    }
    public void setDealerName(String dealerName) {
        this.dealerName = dealerName;
    }

    public SODetailsEmbeddable getSoDetailsEmbeddable() {
        return soDetailsEmbeddable;
    }
    public void setSoDetailsEmbeddable(SODetailsEmbeddable soDetailsEmbeddable) {
        this.soDetailsEmbeddable = soDetailsEmbeddable;
    }
    public Date getSoDate() {
        return soDate;
    }
    public void setSoDate(Date soDate) {
        this.soDate = soDate;
    }
    public boolean isBillGenerated() {
        return isBillGenerated;
    }
    public void setBillGenerated(boolean isBillGenerated) {
        this.isBillGenerated = isBillGenerated;
    }
    public boolean isLDAttached() {
        return isLDAttached;
    }
    public void setLDAttached(boolean isLDAttached) {
        this.isLDAttached = isLDAttached;
    }
    public Set<SOItemDetails> getSetSOItemDetails() {
        return setSOItemDetails;
    }
    public void setSetSOItemDetails(Set<SOItemDetails> setSOItemDetails) {
        this.setSOItemDetails = setSOItemDetails;
    }
    @Override
    public String toString() {
        return"SODetails [soDetailsEmbeddable=" + soDetailsEmbeddable +", lprNo=" + lprNo +", dealerName="
                + dealerName +", soDate=" + soDate +", vat=" + vat +", noOfQuotations=" + noOfQuotations
                +", linkedSO=" + linkedSO +", billHead=" + billHead +", billCategory=" + billCategory +", noOfCSTs="
                + noOfCSTs +", noOFCRVs=" + noOFCRVs +", sOGrandTotalAmount=" + sOGrandTotalAmount
                +", sOGrandTotalRoundOfAmount=" + sOGrandTotalRoundOfAmount +", billGrandTotalAmount="
                + billGrandTotalAmount +", billGrandTotalRoundOffAmount=" + billGrandTotalRoundOffAmount
                +", isBillGenerated=" + isBillGenerated +", isShortClosedSO=" + isShortClosedSO
                +", isShortClosedGenerated=" + isShortClosedGenerated +", isLDAttached=" + isLDAttached
                +", lDAmount=" + lDAmount +", itemDueDate=" + itemDueDate +", noOfWeeksDelay=" + noOfWeeksDelay
                +", ldPercentage=" + ldPercentage +", finalAmountAfterLD=" + finalAmountAfterLD
                +", amountOnWhichLDCalculated=" + amountOnWhichLDCalculated +", setSOItemDetails=" + setSOItemDetails
                +"]";
    }
}

SOItemDetails 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package mco.com.billing.app.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.Table;

@Entity
@Table(name="SO_ITEM_DTLS")
public class SOItemDetails {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name ="SO_ITEM_DTLS_ID", unique = true, nullable = false, length = 100)
    String soItemDtlsRecordNo;

    @Column(name ="S_NO", nullable = false, length = 100)
    String itemSNo;

    @Column(name ="ITEM_UNIT_TYPE", nullable = false, length = 100)
    String itemUnitType;

    @Column(name ="ITEM_NOMENCLATURE", nullable = false, length = 400)
    String itemNomenclature;

    @Column(name ="FOR_QUANTITY", nullable = false, length = 100)
    String forQuantity;

    @Column(name ="READ_QUANTITY", length = 100)
    String readQuantity;

    @Column(name ="FOR_AMOUNT", nullable = false, length = 100)
    String forAmount;

    @Column(name ="READ_AMOUNT", length = 100)
    String readAmount;

    @Column(name ="SUPPLY_DATE", length = 100)
    Date supplyDate;

    @Column(name ="PRICE", nullable = false, length = 100)
    String price;

    @Column(name ="IS_LD_ITEM")
    boolean isLDItem;

    @MapsId("soDetailsEmbeddable")
    @JoinColumns({@JoinColumn(name="SO_NO_FK", referencedColumnName="SO_NO"),
        @JoinColumn(name="FIN_YEAR_FK", referencedColumnName="FINANCIAL_YEAR")
    })
    @ManyToOne(fetch=FetchType.LAZY)
    private SODetails sODetails;

    public String getSoItemDtlsRecordNo() {
        return soItemDtlsRecordNo;
    }

    public boolean isLDItem() {
        return isLDItem;
    }


    public void setLDItem(boolean isLDItem) {
        this.isLDItem = isLDItem;
    }


    public void setSoItemDtlsRecordNo(String soItemDtlsRecordNo) {
        this.soItemDtlsRecordNo = soItemDtlsRecordNo;
    }

    public String getItemSNo() {
        return itemSNo;
    }

    public void setItemSNo(String itemSNo) {
        this.itemSNo = itemSNo;
    }

    public String getItemUnitType() {
        return itemUnitType;
    }

    public void setItemUnitType(String itemUnitType) {
        this.itemUnitType = itemUnitType;
    }

    public String getItemNomenclature() {
        return itemNomenclature;
    }

    public void setItemNomenclature(String itemNomenclature) {
        this.itemNomenclature = itemNomenclature;
    }

    public String getForQuantity() {
        return forQuantity;
    }

    public void setForQuantity(String forQuantity) {
        this.forQuantity = forQuantity;
    }

    public String getReadQuantity() {
        return readQuantity;
    }

    public void setReadQuantity(String readQuantity) {
        this.readQuantity = readQuantity;
    }

    public String getForAmount() {
        return forAmount;
    }

    public void setForAmount(String forAmount) {
        this.forAmount = forAmount;
    }

    public String getReadAmount() {
        return readAmount;
    }

    public void setReadAmount(String readAmount) {
        this.readAmount = readAmount;
    }

    public Date getSupplyDate() {
        return supplyDate;
    }

    public void setSupplyDate(Date supplyDate) {
        this.supplyDate = supplyDate;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public SODetails getsODetails() {
        return sODetails;
    }

    public void setsODetails(SODetails sODetails) {
        this.sODetails = sODetails;
    }

    @Override
    public String toString() {
        return"SOItemDetails [soItemDtlsRecordNo=" + soItemDtlsRecordNo +", itemSNo=" + itemSNo +", itemUnitType="
                + itemUnitType +", itemNomenclature=" + itemNomenclature +", forQuantity=" + forQuantity
                +", readQuantity=" + readQuantity +", forAmount=" + forAmount +", readAmount=" + readAmount
                +", supplyDate=" + supplyDate +", price=" + price +", isLDItem=" + isLDItem +", sODetails="
                + sODetails +"]";
    }
}

SODetails可嵌入类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package mco.com.billing.app.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class SODetailsEmbeddable implements Serializable{

    private static final long serialVersionUID = 1L;

    @Column(name ="SO_NO", nullable = false, length = 100)
    String soNo;

    @Column(name="FINANCIAL_YEAR",  length=100)
    String financialYear;

    public String getSoNo() {
        return soNo;
    }

    public void setSoNo(String soNo) {
        this.soNo = soNo;
    }

    public String getFinancialYear() {
        return financialYear;
    }

    public void setFinancialYear(String financialYear) {
        this.financialYear = financialYear;
    }

    @Override
    public String toString() {
        return"SODetailsEmbeddable [soNo=" + soNo +", financialYear=" + financialYear +"]";
    }
}

事务逻辑功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public void saveSOWithBillingData(SODetails soDetails) {
        Session session=null;
        Transaction tx=null;
        try{
            SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
            session=sessionFactory.openSession();
            tx=session.beginTransaction();
            session.save(soDetails);
            tx.commit();
        }catch(HibernateException ex){
            try{
                if(null!=tx)
                    tx.rollback();
            }catch(RuntimeException e){
                e.printStackTrace();
            }
            throw ex;
        }catch(RuntimeException ex){
            throw ex;
        }finally{
            if(null!=session)
                session.close();
        }
    }

  • 嗨,Sathish,您正在编写事务逻辑。您能否也粘贴该逻辑。
  • <property name="hibernate.hbm2ddl.auto">update</property> 因为如果存在它会删除并重新创建。
  • @Pradeep 更新了事务逻辑
  • @Pradeep 更新不起作用


我的错,这只是一个愚蠢的错误。未创建 SOItemDetails 表,因为 @GeneratedValue 不适用于 String 类型,它应该是 int 类型。现在需要在 SOItemDetails 实体类中进行以下更改。

1
2
3
4
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name ="SO_ITEM_DTLS_ID", unique = true, nullable = false, length = 100)
    int soItemDtlsRecordNo;

有关关于 java:Table not getting created using Hibernate Automatic Query Generation的更多相关文章

  1. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  2. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  3. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  4. java - 什么相当于 ruby​​ 的 rack 或 python 的 Java wsgi? - 2

    什么是ruby​​的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht

  5. Observability:从零开始创建 Java 微服务并监控它 (二) - 2

    这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/

  6. 【Java 面试合集】HashMap中为什么引入红黑树,而不是AVL树呢 - 2

    HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候

  7. 【Java入门】使用Java实现文件夹的遍历 - 2

    遍历文件夹我们通常是使用递归进行操作,这种方式比较简单,也比较容易理解。本文为大家介绍另一种不使用递归的方式,由于没有使用递归,只用到了循环和集合,所以效率更高一些!一、使用递归遍历文件夹整体思路1、使用File封装初始目录,2、打印这个目录3、获取这个目录下所有的子文件和子目录的数组。4、遍历这个数组,取出每个File对象4-1、如果File是否是一个文件,打印4-2、否则就是一个目录,递归调用代码实现publicclassSearchFile{publicstaticvoidmain(String[]args){//初始目录Filedir=newFile("d:/Dev");Datebeg

  8. java - 为什么 ruby​​ modulo 与 java/other lang 不同? - 2

    我基本上来自Java背景并且努力理解Ruby中的模运算。(5%3)(-5%3)(5%-3)(-5%-3)Java中的上述操作产生,2个-22个-2但在Ruby中,相同的表达式会产生21个-1-2.Ruby在逻辑上有多擅长这个?模块操作在Ruby中是如何实现的?如果将同一个操作定义为一个web服务,两个服务如何匹配逻辑。 最佳答案 在Java中,模运算的结果与被除数的符号相同。在Ruby中,它与除数的符号相同。remainder()在Ruby中与被除数的符号相同。您可能还想引用modulooperation.

  9. java - Ruby 相当于 Java 的 Collections.unmodifiableList 和 Collections.unmodifiableMap - 2

    Java的Collections.unmodifiableList和Collections.unmodifiableMap在Ruby标准API中是否有等价物? 最佳答案 使用freeze应用程序接口(interface):Preventsfurthermodificationstoobj.ARuntimeErrorwillberaisedifmodificationisattempted.Thereisnowaytounfreezeafrozenobject.SeealsoObject#frozen?.Thismethodretur

  10. ruby-on-rails - 关于 Ruby 的一般问题 - 2

    我在我的rails应用程序中安装了来自github.com的acts_as_versioned插件,但有一段代码我不完全理解,我希望有人能帮我解决这个问题class_eval我知道block内的方法(或任何它是什么)被定义为类内的实例方法,但我在插件的任何地方都找不到定义为常量的CLASS_METHODS,而且我也不确定是什么here,并且有问题的代码从lib/acts_as_versioned.rb的第199行开始。如果有人愿意告诉我这里的内幕,我将不胜感激。谢谢-C 最佳答案 这是一个异端。http://en.wikipedia

随机推荐