我写了一个教育项目。这是一个简单的聊天。后端技术的堆栈是Java,Jetty(服务器,websocket),Hibernate,PostgreSQL。两个从属实体存在一些注释问题。第一个实体是
@Table(name = "users") //public class UsersDataSet
......
@Id
@SequenceGenerator(name = "userIdSequence", sequenceName = "user_id_sequence", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "userIdSequence")
@Column(name = "user_id", nullable = false, updatable = false)
private long id;
@OneToOne(optional = false, cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn(name = "user_one")
private ConversationDataSet userOneConversationDataSet;
@OneToOne(optional = false, cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn(name = "user_two")
private ConversationDataSet userTwoConversationDataSet;
public UsersDataSet() {
}
//getters, setters...
第二个是
@Table(name = "conversation") //public class ConversationDataSet
......
@Id
@SequenceGenerator(name = "cIdSequence", sequenceName = "c_id_sequence", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "cIdSequence")
@Column(name = "c_id", nullable = false, updatable = false)
private long cId;
@Column(name = "user_one", nullable = false)
private long userOne;
@Column(name = "user_two", nullable = false)
private long userTwo;
@OneToOne(optional = false, mappedBy = "userOneConversationDataSet")
private UsersDataSet userOneDataSet;
@OneToOne(optional = false, mappedBy = "userTwoConversationDataSet")
private UsersDataSet userTwoDataSet;
public ConversationDataSet(){
}
public ConversationDataSet(long userOne, long userTwo){
this.userOne = userOne;
this.userTwo = userTwo;
}
//getters, setters...
SQL shema是
create table users
(
user_id bigint not null primary key
);
create sequence user_id_sequence start with 1 increment by 1 no cycle owned by users.user_id;
create table conversation
(
c_id bigint not null primary key,
user_one bigint not null,
user_two bigint not null,
constraint fk_user_one foreign key (user_one) references users(user_id),
constraint fk_user_two foreign key (user_two) references users(user_id)
);
create sequence c_id_sequence start with 1 increment by 1 no cycle owned by conversation.c_id;
当我尝试在session.save(new ConversationDataSet(userOne, userTwo))
这样的数据库中保存新的ConversationDataSet对象(“ conversation”)时,出现以下错误
org.hibernate.PropertyValueException: not-null property references a null or transient value : dbService.dataSets.ConversationDataSet.userOneDataSet
at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:108)
at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:56)
at org.hibernate.action.internal.AbstractEntityInsertAction.nullifyTransientReferencesIfNotAlready(AbstractEntityInsertAction.java:115)
at org.hibernate.action.internal.AbstractEntityInsertAction.makeEntityManaged(AbstractEntityInsertAction.java:124)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:289)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:263)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:250)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:367)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:292)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:200)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:143)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:709)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:701)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:696)
at dbService.dao.ConversationDAO.save(ConversationDAO.java:46)
at dbService.DBServiceImpl.setConversation(DBServiceImpl.java:82)
at servlets.UsersServlet.doPut(UsersServlet.java:78)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:865)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:535)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1317)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1219)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144)
at org.eclipse.jetty.server.handler.HandlerList.handle(HandlerList.java:61)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:531)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:352)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:281)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:762)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:680)
at java.lang.Thread.run(Thread.java:745)
对于该问题的帮助,我将不胜感激。谢谢。
答案 0 :(得分:0)
userOneDataSet
和userTwoDataSet
的注释上都带有optional = false
。这等效于NOT NULL。您正在尝试将具有这两个属性的实例保存为null。