Home > Software design >  Hibernate PropertyAccessException. Spring dependencies problem
Hibernate PropertyAccessException. Spring dependencies problem

Time:05-31

My application originally used only Hibernate and everything worked fine, but with Spring i'm getting an exception. It feels like Spring causes different behavior for Hibernate.

So with Spring & Hibernate i get this exception trace:

Stack trace photo1

Stack trace photo1

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
      <!-- Database connection settings -->
          <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
          <property name="connection.url">jdbc:mysql://localhost:3306/IndexingAndSearching? 
    serverTimezone=UTC</property>
    <property name="connection.CharSet">utf8</property>
    <property name="connection.characterEncoding">utf8</property>
    <property name="connection.useUnicode">true</property>
    <property name="connection.username">test</property>
    <property name="connection.password">test</property>

    <property name="connection.pool_size">20</property>
    <property name="dialect">org.hibernate.dialect.MySQL57Dialect</property>
    <property name="current_session_context_class">thread</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="show_sql">false</property>
    <property name="hbm2ddl.auto">validate</property>
</session-factory>
</hibernate-configuration>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven- 
4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

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

<groupId>IndexingAndSearching</groupId>
<artifactId>IndexingAndSearching</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<properties>
    <java.version>11</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
<!--   Lucene    -->
<dependency>
    <groupId>org.apache.lucene</groupId>
    <artifactId>lucene-core</artifactId>
    <version>9.0.0</version>
</dependency>
<dependency>
    <groupId>org.apache.lucene</groupId>
    <artifactId>lucene-analyzers-common</artifactId>
    <version>8.11.1</version>
</dependency>
<dependency>
    <groupId>org.apache.lucene</groupId>
    <artifactId>lucene-queryparser</artifactId>
    <version>9.0.0</version>
</dependency>

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.9.0</version>
</dependency>

<!--   Hibernate    -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.6.7.Final</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.28</version>
</dependency>

<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.14.3</version>
</dependency>


<!--   Logger    -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.36</version>
</dependency>
<!--   JUnit    -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>RELEASE</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.1-jre</version>
</dependency>

<!--   Spring    -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

All of my DAO methods for mysql are written with sessionFactory

Links - my only table

@Entity(name = "links")
@Table(name = "links")
@DynamicInsert // To set initial value of fields, otherwise doesn't work
public class Links implements Serializable {
@Id
private String link;
@Enumerated
@Column(columnDefinition = "smallint")
private Status status;
@Version
private int version;
private int depth;
private long owner;

public Links() {
} // hibernate requirement

public Links(String link, Status status, int depth) throws URISyntaxException {
    assert depth >= 0;
    assert !LinkParser.isBlank(link);

    this.link = link;
    this.status = status;
    this.depth = depth;
    this.owner = 0;
}

public String getLink() {
    return link;
}

private void setLink(String link) {
    this.link = link;
}

public int getVersion() {
    return version;
}

private void setVersion(int version) {
    this.version = version;
}

public Status getStatus() {
    return status;
}

public void setStatus(Status status) {
    this.status = status;
}

public int getDepth() {
    return depth;
}

private void setDepth(int depth) {
    this.depth = depth;
}

public long getOwner() {
    return owner;
}

public void setOwner(long owner) {
    assert owner >= 0;
    this.owner = owner;
}

application.properties

server.port=8080
server.servlet.context-path=/webcrawler

Only difference is when i start application with Spring, i dont get these exceptions when i dont use Spring.

CodePudding user response:

I think the issue is from the fact that you setter is private and should be public

private void setLink(String link) {
    this.link = link;
}

Should be

public void setLink(String link) {
    this.link = link;
}

Same for setVersion and setDepth

CodePudding user response:

I found out, I commented these maven dependencies

<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-devtools</artifactId>-->
<!--            <scope>runtime</scope>-->
<!--            <optional>true</optional>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-test</artifactId>-->
<!--            <scope>test</scope>-->
<!--        </dependency>-->

And everything started working... I guess devtools are the reason, dont want to check, i tried so many other stuff, and this is the reason...

  • Related