I am trying to implement hibernate Single table inheritance for a small project. Im using mariadb and springboot. I have defined a table called
xpense_raw_transactions
which is my single tabled for all bank transactions.
below is the table definition
DROP TABLE IF EXISTS `xpense_raw_transactions`;
CREATE TABLE IF NOT EXISTS `xpense_raw_transactions`(
`bank` varchar(31) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`transactionDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`narration` varchar(1024) NOT NULL,
`chqRefNo` varchar(256) DEFAULT NULL,
`fkSubXpenseCategory` int(11) DEFAULT NULL,
`withdrawalAmt` DECIMAL(10,2),
`depositAmt` DECIMAL(10,2),
`closingBalance` DECIMAL(10,2) DEFAULT 0,
PRIMARY KEY (`id`)
);
here bank
is the discriminator column.
When I persist the objects of the subclass, same columns are created with "_".
here is my spring jdbc configuration
#database properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/xpense
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
Here is my parent class
import lombok.*;
import javax.persistence.*;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
@Entity
@Table(name="xpense_raw_transactions")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="bank",
discriminatorType = DiscriminatorType.STRING)
public class DefaultRawTransaction {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;
@Column(name="transactionDate")
private Date transactionDate;
@Column(name="withdrawalAmt")
private Double withdrawalAmt;
@Column(name="depositAmt")
private Double depositAmt;
@Column(name="closingBalance")
private Double closingBalance;
@Column(name="fkSubXpenseCategory")
private Integer fkSubXpenseCategory;
}
here is my sub class
package com.xpense.services.app.fileprocessing;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@DiscriminatorValue(value = "hdfc")
public class HdfcRawTransaction extends DefaultRawTransaction {
@Column(name="narration")
private String narration;
@Column(name="chqRefNo")
private String chqRefNo;
}
my jpa repository classes are as below:
@NoRepositoryBean
public interface DefaultRawTransactionRepository extends CrudRepository<DefaultRawTransaction,Integer> {
}
@Repository
public interface HdfcRawTransactionRepository extends DefaultRawTransactionRepository {
}
in my service class, I'm saving a list of my subclass objects
@Override
public <S extends DefaultRawTransaction> List<S> uploadStatement(String type) {
List<HdfcRawTransaction> transactions = new ArrayList<>();
if ("PDF".equals(type)) {
transactions = pdfFileProcessor.loadPdfFile(true);
transactions = (List<HdfcRawTransaction>) hdfcRawTransactionRepository.saveAll(transactions);
}
return (List<S>) transactions;
}
when I save, new columns are created and the data is persisted. I want the data to be stored as per my table defination.
Is there something wrong? please help