I have no idea what you are trying to achieve, but since the primary key column is named id
, you don’t need @PrimaryKeyJoinColumn
. The documentation states this is only necessary when you use the primary key as foreign key: https://docs.oracle.com/javaee/5/api/javax/persistence/PrimaryKeyJoinColumn.html
If you want to map an enum, you have to use the @Enumerated
annotation or a converter since an enum can be mapped in various ways in a database. Try the following:
@Table(name = "orders")
@Entity
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@OneToOne(cascade = CascadeType.ALL, optional = false)
@JoinColumn(name = "pet_id", referencedColumnName = "id")
private Pet pet;
private Date orderTimestamp;
@Enumerated
private OrderStatus status;
private boolean complete;
}
Not sure if you are aware, but @OneToOne
imposes that the join column, pet_id
in this cases, is unique i.e. a unique constraint exists on the database, so there can only exist one Order
per Pet
. If you don’t want that, use @ManyToOne
.
I have learnt from many tutorials that OneToOne is the way to go for my case but I am not sure why whenever I created a Oder endPoint in my controller and the PetId already in the database, it won’t recognise the PetId at all.
I have no idea what you mean by “recognise the PetId”, so could you please show me what you are doing?
Show the code, tell me what you want to achieve. What is it that doesn’t work? What are your expectations or what did you try so far?