Search code examples
spring-bootspring-data-jpahibernate-mapping

Spring Data JPA - one to one mapping bidirectional


I have a table called symbols and symbol history,

 Symbols
    symbols(Pk)
    
 SymbolsHistorical
    id
    symbols
    Forign key name - symbols_fk (FK)

I created my HibernateMapping and in my DTO classes,

In SymbolsHistorical,

@OneToOne
@JoinColumn(foreignKey = @ForeignKey(name = "symbols_fk"))
private Symbols symbol;

I did in Symbols class,

@OneToOne(mappedBy = "symbol")
private SymbolsHistorical symbolsHistorical;

Whenever I need to read symbols, I need its symbolsHistorical as well,

Iam using findBySymbol method to get symbolsData. Whenever I do the above mapping, am getting, Unknown column 'symbols0_.symbols_historical_id' in 'field list'

What am doing wrong?

Update:

Error 
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:755)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:755)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:755)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:755)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:755)

[2m2021-07-05 15:31:29.458[0;39m [31mERROR[0;39m [35m76390[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36ms.e.ErrorMvcAutoConfiguration$StaticView[0;39m [2m:[0;39m Cannot render error page for request [/symbols/A] and exception [] as the response has already been committed. As a result, the response may have the wrong status code.

Solution

  • Taken from your tables the foreign key name is symbols.

    So the mapping must be in Symbols class:

    @OneToOne
    @JoinColumn(name="symbols")
    private Symbols symbol;
    

    As you are using JSON serialization you will get a StackOverflowError because of the bi-directional mapping.

    So you have to add an annotation to the Symbol or SymbolsHistorical class (depending on what you want to serialize) to break the loop.

    For example

    @JsonBackReference // this property will not be serialized
    @OneToOne(mappedBy = "symbol")
    private SymbolsHistorical symbolsHistorical;