0

I'm tried to create one-to-one relationship in hibernate, When I'm implement this relation then I'm getting org.hibernate.AnnotationException. Please identify the mistake And Suggest me.

Vegetable Class

@SuppressWarnings("serial")
@Entity
@Table(name = "Vegetables")
@Access(value = AccessType.FIELD)
public class Vegetable implements Serializable  {

    @Id
    @Column(name = "P_ID")
    private String productID;

    @Column(name = "Product_Name")
    private String productName;

    @Column(name = "Description")
    private String description;

    @Column(name = "In_Item")
    private LocalDate localDateIn;

    @Column(name = "Out_Item")
    private LocalDate localDateOut;

    @OneToOne(mappedBy = "vegetable", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
        private Stock stock;
//getter & setter
}

Stock Class

@SuppressWarnings("serial")
@Entity
@Table(name="STOCK")
@Access(value=AccessType.FIELD)
public class Stock implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer sId;

    @Column(name="stockLevel")
    private Integer stockLevel;

    @Column(name="StockUSed")
    private Integer stockUsed;

    @Column(name="Availablity")
    private Integer availablity;

    @Column(name="OutOfStock")
    private Integer outOfStock;

    //getter & setter
}

Exception

Caused by: org.hibernate.AnnotationException: Unknown mappedBy in: com.javabootstar.grofers.domain.Vegetable.stock, referenced property unknown: com.javabootstar.grofers.domain.Stock.vegetable
0

1 Answer 1

3

You are not making the correct usage of mappedBy attribute in @OneToOne. MappedBy attributes communicates that the key for the relationship is on the other side.

In your case it is expecting the vegetable field in your Stock class(which is unavailable), that is why you are facing this exception.

You should use @JoinColumn annotation to map foreign key in your Vegetable class

Sign up to request clarification or add additional context in comments.

7 Comments

I wil add @JoinColumn but same exception will be occur .
@OneToOne(mappedBy = "vegetable", cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinColumn(name = "Vegetable_Stock") private Stock stock;
You should not use mappedBy = "vegetable" attribute as there is no vegetable field in your Stock class. Simply using @JoinColumn (with correct values) would suffice
Okay I will remove it.
Unknown MappedBy Remove but new Exception raised OneToOne or ManyToOne on com.javabootstar.grofers.domain.Vegetable.stock references an unknown entity: com.javabootstar.grofers.domain.Stock
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.