Angular 5:选择具有相关角色OneToMany Relationship的所有用户

时间:2018-06-18 22:13:35

标签: hibernate typescript spring-boot angular5

我正在尝试选择具有相关角色的所有用户,因为他们知道一个角色可以与多个联系人相关,而一个联系人只有一个角色。

我尝试了下面的代码,但它只返回一个空白表,这是hibernate的问题还是我如何调用公司名称列?

ContactRepository.java

@Query("SELECT c, cmp.name FROM Contact c, Company cmp WHERE c.company=cmp.id")
public Page<Contact> search(Pageable page);

ContactRestService

 @RequestMapping(value="/searchContacts", method=RequestMethod.GET)
    @ResponseBody
    public Page<Contact> search(
            @RequestParam(name="page",defaultValue="0") int page,
            @RequestParam(name="size",defaultValue="5") int size){
        return contactRepository.search(PageRequest.of(page, size));    
    }

contacts.component.html

<tr *ngFor="let c of pageContacts?.content">
    <td class="py-1">
        <img src="../../assets/images/faces-clipart/pic-1.png" alt="image"/>
    </td>
    <td>{{c.email}}</td>
    <td>{{c.phone}}</td>
    <td>{{c.firstName}}</td>
    <td>{{c.lastName}}</td>
    <td>{{c.job}}</td>
    <td>{{c.birthday}}</td>
    <td>{{c.company.name}}</td>
</tr>

contact.service.ts

@Injectable()
export class ContactsService {

  constructor(private http:HttpClient){}

  getContacts(){
    return this.http.get("http://localhost:8080/searchContacts?page=0&size=6")
  }

}

contacts.component.ts

@Component({
  selector: 'app-contacts',
  templateUrl: './contacts.component.html',
  styleUrls: ['./contacts.component.css']
})
export class ContactsComponent implements OnInit {

  pageContacts:any;
  constructor(private contactsService:ContactsService) { }

  ngOnInit() {
    console.log("Initialisation......");
    this.contactsService.getContacts()
      .subscribe(data =>{
        console.log(data);
        this.pageContacts=data;
      }, err=>{
      console.log("ALERT!!!!!"+err);
      })
  }

}

1 个答案:

答案 0 :(得分:0)

实际上响应是一个数组,所以当我将代码改为此时它可以工作:

<tr *ngFor="let c of pageContacts?.content">
    <td class="py-1">
        <img src="../../assets/images/faces-clipart/pic-1.png" alt="image"/>
    </td>
    <td>{{c[0].email}}</td>
    <td>{{c[0].phone}}</td>
    <td>{{c[0].firstName}}</td>
    <td>{{c[0].lastName}}</td>
    <td>{{c[0].job}}</td>
    <td>{{c[0].birthday}}</td>
    <td>{{c[1]}}</td>
</tr>