so I have an hibernate Entity called Appointment, in this entity I have a AppointNumber property which itself contains a number property which is a string. When I persist my Appointment, I need the AppointmentNumber. I got it to work with @Embedded and @Embeddable the other day but this creates a join table Which I can't have. I tried many other solutions to try and get it to work without join tables but I can't figure it out. (I get lots of ava.lang.IllegalStateException) Can anyone help? Thanks!
@Entity(name = "appointments")
public class Appointment {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@OneToOne(mappedBy = "number")
@Fetch(value = FetchMode.SELECT)
private AppointmentNumber appointmentNumber;
AppointmentNumber, used in Appointment but should not be an entity
public class AppointmentNumber {
@OneToOne
@JoinColumn(name = "appointmentNumber", unique = true, nullable = false)
private String number;
You could do like this:
@Entity(name = "appointments")
public class Appointment {
///....
@Convert(converter = AppointmentNumberConverter.class)
private AppointmentNumber appointmentNumber;
///....
}
@Converter
public class AppointmentNumberConverter implements
AttributeConverter<PersonName, String> {
@Override
public String convertToDatabaseColumn(AppointmentNumber appointmentNumber) {
if (appointmentNumber == null) {
return null;
}
return appointmentNumber.getNumber();
}
@Override
public AppointmentNumber convertToEntityAttribute(String appointmentNumber) {
if (appointmentNumber == null) {
return null;
}
AppointmentNumber result = new AppointmentNumber();
result.setNumber(appointmentNumber);
return result;
}
}
Have a look at JPA Converter documentation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.