我正在开发一个Spring-MVC应用程序,它在数据库中有2个表和2个域类。 Class Person与类Notes具有一个TOMany关系。我想在数据库中添加Person和notes。所以我用谷歌搜索,找出许多基于MVC的例子来解决同样的问题。然而,他们似乎假设了一些事情:
所以基本上,我找到了具有不同名称和开发人员的类似示例。我的问题是:
错误:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.journaldev.spring.model.Person
org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:294)
org.hibernate.type.EntityType.getIdentifier(EntityType.java:537)
org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:311)
org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:321)
org.hibernate.type.TypeHelper.findDirty(TypeHelper.java:294)
人物模型:
@Entity
@Table(name="person")
public class Person implements UserDetails{
private static final GrantedAuthority USER_AUTH = new SimpleGrantedAuthority("ROLE_USER");
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "person_seq_gen")
@SequenceGenerator(name = "person_seq_gen",sequenceName = "person_seq")
private int id;
@OneToMany(cascade = CascadeType.ALL,mappedBy = "person1")
private Set<Notes> notes1;
public Set<Notes> getNotes1() {
return notes1;
}
public void setNotes1(Set<Notes> notes1) {
this.notes1 = notes1;
}
笔记型号:
@Entity
@Table(name="note")
public class Notes {
@Id
@Column(name="noteid")
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "note_gen")
@SequenceGenerator(name = "note_gen",sequenceName = "note_seq")
private int noteId;
@ManyToOne
@JoinColumn(name = "id")
private Person person1;
public Person getPerson1() {
return person1;
}
public void setPerson1(Person person1) {
this.person1 = person1;
}
NotesDAOImpl:
@Transactional
@Repository
public class NotesDAOImpl implements NotesDAO{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf){
this.sessionFactory = sf;
}
@Override
public void addNote(Notes notes, int id) {
Session session = this.sessionFactory.getCurrentSession();
session.save(notes);
}
SQL架构:
CREATE TABLE public.person (
id INTEGER NOT NULL,
firstname VARCHAR,
username VARCHAR,
password VARCHAR,
CONSTRAINT personid PRIMARY KEY (id)
);
CREATE TABLE public.note (
noteid INTEGER NOT NULL,
sectionid INTEGER,
canvasid INTEGER,
text VARCHAR,
notecolor VARCHAR,
noteheadline VARCHAR,
id INTEGER NOT NULL,
CONSTRAINT noteid PRIMARY KEY (noteid)
);
ALTER TABLE public.note ADD CONSTRAINT user_note_fk
FOREIGN KEY (id)
REFERENCES public.person (id)
ON DELETE NO ACTION
ON UPDATE NO ACTION
NOT DEFERRABLE;
顺便说一句,addNote方法中的id只是检查SpringSecurity是否实际发送了userid,并且已经正确登录,调试目的。
所以,一旦用户登录,我无法添加备注,我做错了什么?或者Hibernate无法做到这一点。在那种情况下,让我找一把枪射击自己......:P
答案 0 :(得分:1)
您的代码会尝试保存笔记。但这些笔记不会与任何人联系在一起。你必须在下面的操作顺序。
以下是代码模板。
@Transactional
@Repository
public class NotesDAOImpl implements NotesDAO{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf){
this.sessionFactory = sf;
}
@Override
public void addNote(Notes notes, int id) {
Session session = this.sessionFactory.getCurrentSession();
Person person = getPerson(); // this method should get logged in person or the person for whom you want to save the notes.
if (person.getNotes() == null) {
Set<Note> notes = new HashSet<Note>();
person.setNotes(notes);
}
person.getNotes().add(note);
note.setPerson(person); // If bidirectional relationship.
session.update(person); // if update does not work, try merge();
}
还要确保在notes字段中将cascade类型设置为MERGE in person实体。
注意:上面的代码只是代码中的示例,可能有一些编译错误。请根据您的要求更正。