协会不在Spring Boot中工作

时间:2016-11-24 22:04:48

标签: spring spring-boot

我是春天的新手,但过去几周学到了很多东西。我有一个Spring Boot项目,它可以很好地处理简单的资源,但是一旦我做了关联,有些东西就不能按预期工作了。

我的简化应用程序包含资源事件和邀请,其中一个事件可以有多个邀请。

现在我有两个问题:

  1. 当我在API上调用事件时,我会收到事件,但输出中没有邀请。我在MySqlLogs中看到,有一个查询查询邀请,当我将这个查询复制并发送到我的mysql客户端时,我得到了很多结果

  2. 活动和邀请之间没有加入。有一个事件查询,然后一个查询结果中每个事件的邀请。但我希望在这里有一个JOIN,以便只有一个查询。我试过这些宣言

    @OneToMany(mappedBy =“event”,fetch = FetchType.EAGER) @Fetch(FetchMode.JOIN)语句

  3. 但他们没有任何区别。有谁知道为什么我的API输出中没有邀请以及为什么有单个查询而不是JOIN?

    非常感谢你的帮助。

    我看到的示例输出是:

    {
        content: [
          {
            id: 374223,
            start: "2016-12-12 17:30",
            end: "2016-12-12 20:00",
            name: "Hallentraining",
            slug: "hallentraining-501-2016-12-12",
            invitations: [
                {
                    id: 0,
                    message: null,
                    invitationState: 1,
                    memberId: null,
                    version: 0
                }
            ],
            version: 0
           }
         ],
        last: true,
        totalPages: 1,
        totalElements: 1,
        first: true,
        sort: null,
        numberOfElements: 1,
        size: 20,
        number: 0
    }
    

    活动类

    @Entity
    @Component
    @Table(name="event")
    public class Event implements MyEntity {
    
        @Id
        @Column(name="id")
        @GeneratedValue
        private Long id;
    
        private String name;
    
        private String slug;
    
        @Column(name="start")
        @Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm")
        private DateTime start;
    
        @Column(name="end")
        @Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm")
        private DateTime end;
    
        @OneToMany(mappedBy="event", fetch=FetchType.EAGER)
        @Fetch(FetchMode.JOIN)
        private Set<Invitation> invitations = new HashSet<>();
    
    // getters and setters 
    }
    

    邀请班

    @Entity
    @Component
    @Table(name="invitation")
    public class Invitation implements MyEntity {
    
        @Id
        @Column(name="id")
        @GeneratedValue
        private Long id;
    
    
    
        private String message;
    
        @Column(name="invitation_state")
        private Integer invitationState;
    
        @Column(name="member_id")
        private Integer memberId;
    
        @ManyToOne
        @JoinColumn(name="event_id", nullable=false)
        private Event event;
    
        // getters and setters 
    }
    

    EventRepository:

    public interface  EventRepository extends JpaRepository<Event, Long>, JpaSpecificationExecutor {
    
    }
    

    EventController:

    @RestController
    @RequestMapping(value = "/event" , produces="application/json")
    public class EventController {
    
        @Autowired
        private EventRepository repo;
    
        @Autowired
        private EventService eventService;
    
        @RequestMapping(method = RequestMethod.GET)
    
         public Iterable<Event> event(Pageable pageable,
                                     String start ,
                                     String end ,
                                     String name) {
            EventSpecification spec = new EventSpecification(start, end, name );
    
            return repo.findAll( spec, pageable );
        }
    }
    

    我的build.gradle的摘录:

    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.3.6.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '1.3.6.RELEASE'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.5.0'
    compile 'mysql:mysql-connector-java:5.1.34'
    compile group: 'joda-time', name: 'joda-time', version: '2.9.5'
    compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-joda', version: '2.8.4'
    compile group: 'org.jadira.usertype', name: 'usertype.core', version: '3.2.0.GA'
    

0 个答案:

没有答案