dongyi2889 2013-03-21 11:09
浏览 28
已采纳

学说2:无法正确设置OneTo许多关系

I have 2 entities: Interview and Comment. Interview has one to many unidirectional relation with Comment.

Here is my yaml mapping file for Comment:

Entities\Comment:
  type: entity
  table: Comment
  repositoryClass: Repositories\CommentRepository

  fields:
    id:
      type: integer
      id: true
      generator:
        strategy: AUTO
    parentid:
      type: integer
      nullable: false
      column: parentid
    isactive:
      type: integer
      nullable: false
      column: isactive
    isremoved:
      type: integer
      nullable: false
      column: isremoved
    removaldate:
      type: datetime
      nullable: true
      column: removaldate
    user_name:
      type: string
      length: 255
      nullable: false
      column: user_name
    user_email:
      type: string
      length: 255
      nullable: false
      column: user_email
    user_avatar:
      type: string
      length: 255
      nullable: false
      column: user_avatar
    comment:
      type: text
      nullable: false
      column: comment
    creationdate:
      type: datetime
      nullable: false
      column: creationdate
    rating:
      type: integer
      nullable: false

Here is my yaml mapping file for Interview:

Entities\Interview:
  type: entity
  table: Interview
  repositoryClass: Repositories\InterviewRepository

  fields:
    id:
      type: integer
      id: true
      generator:
        strategy: AUTO
    isremoved:
      type: integer
      nullable: false
      column: isremoved
    removaldate:
      type: datetime
      nullable: true
      column: removaldate
    creationdate:
      type: datetime
      nullable: false
      column: creationdate
    rating:
      type: integer
      nullable: false
    anonstitle:
      type: string
      length: 1000
      nullable: false
      column: anonstitle
    anons:
      type: text
      nullable: false
      column: anons
    anonsphoto:
      type: string
      length: 255
      nullable: true
      column: anonsphoto
    interviewtitle:
      type: string
      length: 1000
      nullable: false
      column: interviewtitle
    interview:
      type: text
      nullable: true
      column: interview
    interviewphoto:
      type: string
      length: 255
      nullable: true
      column: interviewphoto
  manyToMany:
    comments:
      targetEntity: Comment
      joinTable:
        name: interviews_comments
        joinColumns:
          interview_id:
            referencedColumnName: id
        inverseJoinColumns:
          comment_id:
            referencedColumnName: id
            unique: true

So after loading schema to database, i have 3 tables. 2 of them are tables of entities, one is for relation and it has just 2 columns: interview_id, comment_id. But after persisting Comment objects for some Interview i dont see anything in join table. Can't find out why.

  • 写回答

1条回答 默认 最新

  • dougu8742 2013-03-23 16:16
    关注

    Interview has one to many unidirectional relation with Comment.

    No, because you defined this relation as manyToMany in Entities\Interview, not unidirectional oneToMany in owning side.

    ManyToMany mapping requeres a join table. Because one Interview can have many Comment and one Comment can have many Interview. This problem cannot be sloved with additional attributes in database tables, so the additional mapping table is created.

    Solution: If you want to have a one Interview with many Comment, but one Comment have one Interview, you have to correct the mapping in yaml Entities\Interview to oneToMany without joinTable (as you defined and it is created).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog