I have LoginCredential
class:
@Data
@Entity
public class LoginCredential implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long userID;
String eMail;
String passwordHash;
@GeneratedValue(strategy = GenerationType.AUTO)
@OneToOne(mappedBy = "loginCredential", fetch = FetchType.LAZY)
User user;
}
And here is my User
class:
@Data
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long userID;
@OneToOne(fetch = FetchType.LAZY,targetEntity = LoginCredential.class)
@JoinColumn(name = "userID",referencedColumnName = "userID")
private LoginCredential loginCredential;
}
And my LoginCredentialController
's POST-ing method is simple :
@PostMapping("/login")
LoginCredential newLoginCredential(@RequestBody LoginCredential newLoginCredential) {
logger.debug(newLoginCredential);
LoginCredential a=repository.save(newLoginCredential);
logger.debug(a);
return a;
}
Now when I tried this command : curl -X POST -H "Content-Type: application/json" -d "{ "email": "1"}" http://localhost:8080/login
I get a LoginCredential
without any error, to be mentioned user
field is null
.
That's why I tried this command curl -X POST -H "Content-Type: application/json" -d "{ "email": "1","user":{} }" http://localhost:8080/login
Which gives me error saying :
{
"status" : 500,
"error" : "Internal Server Error",
"message" : "org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.mua.cse616.Model.LoginCredential.user -> com.mua.cse616.Model.User; org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.mua.cse616.Model.LoginCredential.user -> com.mua.cse616.Model.User",
"trace":....
}
Application.properties
pom.xml
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…