我正在使用Spring-mvc应用程序,该应用程序使用Spring Security作为ORM工具登录和休眠。因此,我的项目只有2个表,Table Person具有表注释的OneToMany映射。因此,一旦用户登录系统,他/她应该能够添加注释,但在添加时我还想保存Person的id,这就是我习惯OneToMany映射的原因。但我不知道如何获取用户的ID并将其放在表单中。以下是我的代码。
错误是
org.postgresql.util.PSQLException: ERROR: null value in column "personid" violates not-null constraint
这是可以理解的,这就是为什么我想知道我如何能够审视这个人格。
人物模型:
@Entity
@Table(name="person")
public class Person implements UserDetails{
private static final GrantedAuthority USER_AUTH = new SimpleGrantedAuthority("ROLE_USER");
@Id
@Column(name="personid")
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "person_seq_gen")
@SequenceGenerator(name = "person_seq_gen",sequenceName = "person_seq")
private int id;
@OneToMany(mappedBy = "person1")
private Set<Notes> 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 = "personid")
private Person person1;
}
SQL:
CREATE TABLE public.person (
personid INTEGER NOT NULL,
firstname VARCHAR,
username VARCHAR,
password VARCHAR,
CONSTRAINT id PRIMARY KEY (personid)
);
CREATE TABLE public.note (
noteid INTEGER NOT NULL,
sectionid INTEGER,
canvasid INTEGER,
text VARCHAR,
notecolor VARCHAR,
noteheadline VARCHAR,
personid INTEGER NOT NULL,
CONSTRAINT noteid PRIMARY KEY (noteid)
);
ALTER TABLE public.note ADD CONSTRAINT user_note_fk
FOREIGN KEY (personid)
REFERENCES public.person (personid)
ON DELETE NO ACTION
ON UPDATE NO ACTION
NOT DEFERRABLE;
人员控制器:
@Controller
public class PersonController {
private PersonService personService;
@Autowired(required=true)
@Qualifier(value="personService")
public void setPersonService(PersonService ps){
this.personService = ps;
}
@RequestMapping(value = "/", method = RequestMethod.GET)
public String listPersons(Model model) {
model.addAttribute("person", new Person());
model.addAttribute("listPersons", this.personService.listPersons());
return "person";
}
//For add and update person both
@RequestMapping(value= "/person/add", method = RequestMethod.POST)
public String addPerson(@ModelAttribute("person") Person p){
//new person, add it
this.personService.addPerson(p);
return "redirect:/";
}
NoteController:
@Controller
public class NoteController {
private NotesService notesService;
@Autowired(required=true)
@Qualifier(value="notesService")
public void setNotesService(NotesService notesService){this.notesService=notesService;}
@RequestMapping(value = "/notes", method = RequestMethod.GET)
public String listNotes(Model model) {
model.addAttribute("notes", new Notes());
model.addAttribute("listnotes", this.notesService.listNotes());
return "notes";
}
@RequestMapping(value= "/note/add", method = RequestMethod.GET)
public String addNote(@ModelAttribute("notes") Notes p){
this.notesService.addNote(p);
return "redirect:/";
}
}
Note.jsp(这是我添加笔记的地方。)
<c:url var="addAction" value="/note/add" ></c:url>
<form:form action="${addAction}" commandName="notes">
<table>
<c:if test="${!empty notes.note}">
<tr>
<td>
<form:label path="noteid">
<spring:message text="noteid"/>
</form:label>
</td>
<td>
<form:input path="noteid" readonly="true" size="8" disabled="true" />
<form:hidden path="noteid" />
</td>
</tr>
</c:if>
<tr>
<td>
<form:label path="note">
<spring:message text="note"/>
</form:label>
</td>
<td>
<form:input path="note" />
</td>
</tr>
<tr>
<td>
<form:label path="notetag">
<spring:message text="notetag"/>
</form:label>
</td>
<td>
<form:input path="notetag" />
</td>
</tr>
<tr>
<td>
<form:label path="notecolor">
<spring:message text="notecolor"/>
</form:label>
</td>
<td>
<form:input path="notecolor" />
</td>
</tr>
<tr>
<td>
<form:label path="canvasid">
<spring:message text="canvasid"/>
</form:label>
</td>
<td>
<form:input path="canvasid" />
</td>
</tr>
<tr>
<td>
<form:label path="sectionid">
<spring:message text="sectionid"/>
</form:label>
</td>
<td>
<form:input path="sectionid" />
</td>
</tr>
<tr>
<td colspan="2">
<c:if test="${!empty notes.note}">
<input type="submit"
value="<spring:message text="Edit note"/>" />
</c:if>
<c:if test="${empty notes.note}">
<input type="submit"
value="<spring:message text="Add note"/>" />
</c:if>
</td>
</tr>
</table>
</form:form>
答案 0 :(得分:1)
不确定我是否理解正确但是当一个线程中有一个登录用户(Principal)时,您可以使用SecurityContextHolder.getContext().getAuthentication().getPrincipal()
来获取Principal或者仅使用@AuthenticationPrincipal
或仅使用public String addNote(@ModelAttribute("notes") Notes p, @AuthenticationPrincipal Person person)) {
p.setPerson1(person);
this.notesService.addNote(p);
return "redirect:/";
}
注入它确保你的Person实现了Principal接口并直接注入它(我假设Person是用户而User是一个Principal)来获取person.id。
在/ note / add中尝试这样:
{{1}}
如果您想避免手动设置,请考虑使用AuditorAware和Auditable,如下所示: http://www.springbyexample.org/examples/spring-data-jpa-auditing-code-example.html