这是我的用户dto
@Entity
@Table(name="userr")
public class Userdata {
@Id
//@GeneratedValue(strategy=GenerationType.AUTO)
private int userid;
@Column(length=20)
private String username;
@OneToOne(targetEntity=Test.class,cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name="testid", referencedColumnName="testId")
private Test usertest;
@Size(min=3, max=20, message="password must be minimum 8 characters")
private String userPassword;
@Min(value=4)
private long userPhoneno;
@Email(message="enter proper email id")
private String userEmail;
@NotBlank(message="usertype cannot be empty")
private String userType;
//getters and setters
这是我的测试dto类
@Entity
@Table(name="test")
public class Test {
@Id
//@GeneratedValue(strategy=GenerationType.AUTO)
private int testId;
@Column(length=50)
private String testTitle;
@OneToMany(mappedBy="test")
private Set<Questions> testQuestions;
private int testTotalMarks;
private int testMarksScored;
//getter and setter
这是Question dto的
@Entity
@Table(name="questions")
public class Questions {
@Id
//@GeneratedValue(strategy=GenerationType.AUTO)
private int questionId;
@Column(length=100)
private String questionTitle;
@Column(length=30)
private String optionOne;
@Column(length=30)
private String optionTwo;
@Column(length=30)
private String optionThree;
@Column(length=30)
private String optionFour;
@Column(length=30)
private int rightAnswer;
@ManyToOne
@JoinColumn(name="testId")
private Test test;
@Column(length=30)
private int questionMarks;
@Column(length=30)
private int choosenAnswer;
@Column(length=30)
private int marksScored;
//getter and setter
}
我的jpa存储库查询
@Query("select t from Userdata u,Test t where u.usertest=t.testId and u.username=:username")
public Test get(@Param("username")String username);
这在邮递员中工作正常,但我在角度上出现错误我的控制器在春季启动]]
@GetMapping("/username/{username}") public Test getUserByName(@PathVariable(value="username") String username) { return user.get(username); }
我的service.ts以角度显示
getTestbyName(u:string): Observable<any>{ const headers =new HttpHeaders().set('Content_Type', 'text/plain ;charset=utf-8'); return this.httpService.get<Test>("http://localhost:8299/user/username/"+ u); }
我使用此服务的用户home.ts
username:string; test:Test[]; constructor(private myservice: MyserviceService, private router: Router, private route:ActivatedRoute) {} ngOnInit(): any { let id=this.route.snapshot.paramMap.get('id'); this.username=id; console.log(this.username); this.myservice.getTestbyName(this.username).subscribe( response => this.handleSuccessfulResponse(response), ); } handleSuccessfulResponse(response) { this.test = response; console.log(response); }
我尝试使用表打印值时,我的user-home.html没有显示并出现错误
<thead>
<tr>
<th>Test ID</th>
<th>Test Title</th>
<th>Test Total Marks</th>
<th>Test</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let e of test">
<td> {{e.testId}} </td>
<td> {{e.testTitle}} </td>
<td> {{e.testTotalMarks}} </td>
</tr>
</tbody>
我收到的错误:错误错误:找不到类型为“对象”的其他支持对象“ [对象对象]”。 NgFor仅支持绑定到Iterables,例如数组。
这是我的用户dto @Entity @Table(name =“ userr”)公共类Userdata {@Id //@GeneratedValue(strategy=GenerationType.AUTO)private int userid; @Column(length = 20)私有字符串用户名; @OneToOne(...
*ngFor
适用于类似数组的集合。要循环对象,可以使用keyvalue
管道。尝试以下]]
keyvalue