Tag Archives: java

[Solved] Hibernate Error: Row was updated or deleted by another transaction

Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) Hibernate Error:

org.hibernate.StaleObjectStateException:Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)

Error reporting reason:

1. The current version is not consistent with the db database version, the data is modified when the value of the form object in the version and the value of the corresponding record in the database version is not consistent, which makes the update method in the call, the version is not consistent with the reverse error.
2. Two or more sessions have modified the same record

Encountering such an exception indicates that the object of the operation uses an optimistic lock or the defined POJO defines the version field

Solution:

1. When submitting the form data, submit the version as well
2. First query the value of the version of the corresponding record in the database and assign it to the entity object to be modified, and then execute the update operation (after modifying the database data, the value of its version will change, so at this time the update operation needs to obtain the latest version value, assign it to the operation object, and then execute the update operation)

[Solved] springboot Project Run Error: HikariPool-1 – Exception during pool initialization.

Let’s take a look at the screenshot of the error report first:
he said that an exception occurred during the initialization of hikaripool-1-pool, which led to the failure of project startup

reason: JDBC connection failed

solution:

Step 1: check the application first After the URL in yaml is 3360/(database name), check whether there is this name in your database
Step 2: check whether the username and password are the same as when designing the database. Usually, we use root and 123456 when designing. Because when you import a new project, these things are easy to forget to change and report errors. So check whether the address, port and database name are the same as your own

in addition, if you are using springboot 2.0 or above

Should be configured as driver ‐ class ‐ Name: com.mysql.cj.jdbc.Driver

 datasource:
    #                              //PATH     PORT   DATABASE_NAME
    url: jdbc:mysql://${MYSQL_HOST:localhost}:3306/studentmanagement
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

After we finish the modification, let’s check the operation again:

The project is running successfully

[Solved] Idea 2021.3 Maven 3.8.1 Error: Blocked mirror for repositories

The blocked mirror for repositories problem is due to Maven 3 8.1 from the beginning, the HTTP connection is blocked in the configuration file by default
idea has the following solutions to solve this problem
1. Replace the use of Maven for version 3.6.3 https://dlcdn.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.zip

2. When idea loads external configuration files, it will load the settings.xml in the \plugins\maven\lib\maven3\conf folder in the installation directory first, comment the file

<mirror>
      <id>maven-default-http-blocker</id>
      <mirrorOf>external:http:*</mirrorOf>
      <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
      <url>http://0.0.0.0/</url>
      <blocked>true</blocked>
    </mirror>

It will take effect

[Solved] POI Read excel Error: Your InputStream was neither an OLE2 stream, nor an OOXML stream

When Java uses POI to read Excel files with XLS suffix, an error is reported:

Your InputStream was neither an OLE2 stream, nor an OOXML stream

Error code:

Workbook wb = WorkbookFactory.create(is);

Click the Create method to see the source code of POI:

public static Workbook create(InputStream inp) throws IOException, InvalidFormatException {
        if (!((InputStream)inp).markSupported()) {
            inp = new PushbackInputStream((InputStream)inp, 8);
        }

        if (POIFSFileSystem.hasPOIFSHeader((InputStream)inp)) {
            return new HSSFWorkbook((InputStream)inp);
        } else if (POIXMLDocument.hasOOXMLHeader((InputStream)inp)) {
            return new XSSFWorkbook(OPCPackage.open((InputStream)inp));
        } else {
            throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
        }
    }

The haspoofshade and hasooxmlreader in the two IFS are to read the first 8 bytes of the excel file stream to determine the file information. If it is not an excel type file or a binary type file, the exception “your InputStream was neither an ole2 stream, nor an OOXML stream” will be thrown directly

Analysis:
open the problematic XLS file with sublime text text editor or Notepad + +, which is XML in text form:

Open other normal XLS files with sublime text text editor as follows, which are binary:

Reason for the problem:
the XLS file in question is actually an office openxml file, also known as spreadsheetml format (XML format of Excel). Its suffix should be XML instead of XLS. It is not a standard excel file, so it will report an error when reading with POI.

Solution:
use Excel to open the problematic XLS file, as follows:
select Yes, then save the file as XLS format, and then use POI analysis to avoid reporting errors

[Solved] Mybatis insert Error: Cause: java.sql.SQLException: SQL String cannot be empty

Mybatis insert error cause: Java sql. SQLException: SQL String cannot be empty

1. Error description

Scenario reproduction: when using mybatis to import a list for batch insertion, the code is as follows:

mapper

void insertTest(List<Test> list);

mapper.xml

<insert id="insertTest" parameterType="java.util.List">
	<if test="list != null and list.size() > 0"> 
		INSERT INTO test (test1, test2)
                VALUES
                <foreach collection="list" index="index" item="item" separator=",">
                    (#{item.test1}, #{item.test2})
                </foreach>
	</if>
</insert>

The reason for the error is that the list is passed in The list with size () 0 causes the SQL statement to be empty and an error is reported

Solution:

1. Make non empty judgment (list! = null &&! List. Isempty()) before using mapper, and set mapper If statement removal in XML

2. Use the choose, when and otherwise tags to judge. If it is empty, give a statement to query the empty string

Specific examples are as follows

	<insert id="insertTest" parameterType="java.util.List">
        <choose>
            <when test="list != null and list.size() > 0">
                INSERT INTO test (test1, test2)
                VALUES
                <foreach collection="list" index="index" item="item" separator=",">
                    (#{item.test1}, #{item.test2})
                </foreach>
            </when>
            <otherwise>
                select ""
            </otherwise>
        </choose>

    </insert>

If the scenario needs to implement the insert statement multiple times, it will not be elegant to judge the space multiple times in the code. You can consider using the following solutions for reference only. If there are better methods, you can exchange and discuss them

[Solved] integrated swagger Start Error: Failed to start bean ‘documentationPluginsBootstrapper‘;

Today, I reported an error when I was going to learn something new and build a new project integration swagger.

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.15.jar:5.3.15]
	at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.15.jar:5.3.15]
	at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.15.jar:5.3.15]
	at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_241]
	at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.15.jar:5.3.15]
	at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.15.jar:5.3.15]
	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.15.jar:5.3.15]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.15.jar:5.3.15]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.3.jar:2.6.3]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) [spring-boot-2.6.3.jar:2.6.3]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:414) [spring-boot-2.6.3.jar:2.6.3]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) [spring-boot-2.6.3.jar:2.6.3]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) [spring-boot-2.6.3.jar:2.6.3]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) [spring-boot-2.6.3.jar:2.6.3]
	at com.hf.logisticsinfo.LogisticsInfoApplication.main(LogisticsInfoApplication.java:10) [classes/:na]
Caused by: java.lang.NullPointerException: null
	at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:112) ~[springfox-spi-2.9.2.jar:null]
	at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:109) ~[springfox-spi-2.9.2.jar:null]
	at com.google.common.collect.ComparatorOrdering.compare(ComparatorOrdering.java:37) ~[guava-20.0.jar:na]
	at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) ~[na:1.8.0_241]
	at java.util.TimSort.sort(TimSort.java:220) ~[na:1.8.0_241]
	at java.util.Arrays.sort(Arrays.java:1438) ~[na:1.8.0_241]
	at com.google.common.collect.Ordering.sortedCopy(Ordering.java:855) ~[guava-20.0.jar:na]
	at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:57) ~[springfox-spring-web-2.9.2.jar:null]
	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:138) ~[springfox-spring-web-2.9.2.jar:null]
	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:135) ~[springfox-spring-web-2.9.2.jar:null]
	at com.google.common.collect.Iterators$7.transform(Iterators.java:750) ~[guava-20.0.jar:na]
	at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47) ~[guava-20.0.jar:na]
	at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47) ~[guava-20.0.jar:na]
	at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:52) ~[guava-20.0.jar:na]
	at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:50) ~[guava-20.0.jar:na]
	at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:249) ~[guava-20.0.jar:na]
	at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:209) ~[guava-20.0.jar:na]
	at com.google.common.collect.FluentIterable.toList(FluentIterable.java:614) ~[guava-20.0.jar:na]
	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.defaultContextBuilder(DocumentationPluginsBootstrapper.java:111) ~[springfox-spring-web-2.9.2.jar:null]
	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.buildContext(DocumentationPluginsBootstrapper.java:96) ~[springfox-spring-web-2.9.2.jar:null]
	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:167) ~[springfox-spring-web-2.9.2.jar:null]
	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-5.3.15.jar:5.3.15]
	... 14 common frames omitted

The general reason is that due to the springboot version problem, the default strategy for matching the request path after 2.6 and spring MVC processing mapping has been changed from antpathmatcher to pathpatternparser.

So the solution here is also very simple

The first solution is to set up spring according to the official prompts mvc.pathmatch.Matching strategy is ant path matcher, which is effective and available for personal testing;

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

The second solution is to directly reduce the springboot version. I’ll try to reduce it to level 2.4 here. There’s no problem

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

[Solved] GRPC-Server Error: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String; CLjava

Grpc server reports an error com google.common.base.Preconditions.checkArgument (ZLjava/lang/String;CLjava/lang/Object);

Problem background solution summary Lyric: I really want to take another bite, ผั๥๥๥ผั๥ณ, ผั๥ณ, ผั๥๥ณ This is the first song. It’s over. Have you guessed the title of the song?

Problem background

When working as grpc server, I can’t start it. The error report is printed as follows, but I can’t well see what’s wrong. Since grpc can be used when I test it alone, but as the project becomes more and more complex, more and more POM dependencies are introduced, so I began to find the reason from it

2022-01-25 11:01:39.896 ERROR [id-mapping-AsyncThread-1] o.s.a.i.SimpleAsyncUncaughtExceptionHandler.handleUncaughtException(SimpleAsyncUncaughtExceptionHandler.java:39): Unexpected exception occurred invoking async method: public void grpc.server.GrpcServer.start() throws java.io.IOException
java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;CLjava/lang/Object;)V
	at io.grpc.Metadata$Key.validateName(Metadata.java:629)
	at io.grpc.Metadata$Key.<init>(Metadata.java:637)
	at io.grpc.Metadata$Key.<init>(Metadata.java:567)
	at io.grpc.Metadata$AsciiKey.<init>(Metadata.java:742)
	at io.grpc.Metadata$AsciiKey.<init>(Metadata.java:737)
	at io.grpc.Metadata$Key.of(Metadata.java:593)
	at io.grpc.Metadata$Key.of(Metadata.java:589)
	at io.grpc.internal.GrpcUtil.<clinit>(GrpcUtil.java:86)
	at io.grpc.internal.AbstractServerImplBuilder.<clinit>(AbstractServerImplBuilder.java:60)
	at io.grpc.netty.shaded.io.grpc.netty.NettyServerProvider.builderForPort(NettyServerProvider.java:39)
	at io.grpc.netty.shaded.io.grpc.netty.NettyServerProvider.builderForPort(NettyServerProvider.java:24)
	at io.grpc.ServerBuilder.forPort(ServerBuilder.java:41)
	at server.Server.start(GrpcServer.java:30)
	at grpc.server.GrpcServer$$FastClassBySpringCGLIB$$be87d0e.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
	at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

 

Solution:

1. Analyze the imported jar package dependency and use file → setting to install Maven dependency helper dependency management

2 after installation, open the POM file, click dependency analyzer

3 select conflicts and click refresh UI to refresh. You can see that guava: version 18.0 appears, which means there is a conflict with this dependency, It’s a repeated introduction,
but the introduction of a problem is to exclude which repeated guava. This problem has been bothering me. My approach is to exclude the displayed dependencies first, and then continue to compile. If not, find other versions of guava for exclusion

4 because there is no exclusion option in Guava in right-click conflicts, Therefore, select jump to left tree to display more clearly

5 exclude 18 versions, re import

6 Click conflicts, and it is found that there is no conflict

7 at that time, the problem that the grpc server cannot be started is also solved

[Solved] IDEA Error: Error running ‘Application‘: Command line is too long

Error running ‘application’ in idea: command line is too long Shorten command line for Application or also for Spring Boot default configuration

Problem background solution 1 (current project settings) solution 2 (global settings)

Lyric: I can’t go to a good school

Command line is too long. Shorten command line for Application or also for Spring Boot default configuration)

Problem background

When idea starts the project, it suddenly reports an error

Error running 'Application':
Command line is too long.Shorten command line for Application or also for Spring Boot default configuration.

Solution:

Method 1 (current project settings)

1 click Edit configurations

in the run drop-down box of the current project. 2 Click environment, select the short command line drop-down box, select classpath file or jar manifest, and click OK to confirm

Method 2 (global setting)

1. Since scheme 1 is the current project setting and other projects are not opened, you can set the global. Click file → new projects settings → run configuration templates for new projects

2. Click springboot project. Other projects are the same as scheme 1

IDEA Error: lombok.extern.slf4j is not exist [How to Solve]

Idea reports an error Lombok extern. Slf4j is not exist no solution exists

Problem background solution summary Lyric: maltose wine

Problem background

When you start the idea project, you suddenly report an error Lombok extern. It’s been a long time since I found that the version of 4fj is compatible with Maven

Solution

1 file → setting

2 build → build tools → maven → runner, check delegate ide build and click OK