Home > Software design >  Transient/Final Variable in Spring boot entity causing Error
Transient/Final Variable in Spring boot entity causing Error

Time:06-19

I am a new to Spring boot so I apologize if this is a easily resolved issue.

I am seeing a problem running springboot after I added a @Transient final variable to my entity. Essentially I need a constant variable in my class that stores a string that I do not want stored in the database.

Adding in just this variable causes no issues with spring and the program runs as expected however, if I add a getter for this variable Springboot raises the exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister

Here is my code causing the issue (I am not even calling the getter yet and it's causing this issue):

@ApiModel(description = "This class represents a device with its basic information.")
@Entity
@Table(name = "device")
public class Device {

    @Transient
    private final String invisibleName = "Jwfqp4bbqiFzRLcFVV3qf";

    @Id
    @ApiModelProperty(notes = "A UUID number to identify the device in the system.")
    private String deviceId;
    @ApiModelProperty(notes = "The device IP address of the device, this is also a unique identifier.")
    private String ipAddress;

    public Device() {

    }

    @Id
    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceUuid) {
        this.deviceId = deviceUuid;
    }

    public String getIpAddress() {
        return ipAddress;
    }

    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

   public String getInvisibleName(){
        return invisibleName;
    }

}

Here is my pom

<?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 https://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.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>project-name</artifactId>
    <version>0.0.1</version>
    <packaging>war</packaging>
    <name>projectName</name>
    <description>Project Name</description>
    <properties>
        <java.version>1.8</java.version>
        <log4j2.version>2.16.0</log4j2.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>compile</scope>
        </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</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

One important thing that I've noticed is that I can resolve the building issue by adding a useless parameter to the the getter such as:

public String getInvisibleName(int temp){
        return invisibleName;
}

but If possible I would like to get to the bottom of this issue so I don't have useless parameters in my getter.

Let me know if I need to provide any additional information.

Thanks

CodePudding user response:

You should move @Transient annotation above the getInvisibleName() method.

CodePudding user response:

Thank you all for your help. I removed the extraneous @Id on the 'getDeviceId' and moved the @Transient to the getter. I also noticed that apparently Spring had created an 'invisibleName' entry in the database causing a mismatch with my @Entity class. I removed the field from the database and that resolved the issue.

  • Related