2016-07-05 80 views
0

我有两个Hibernate对象的“报告”和“ReportContent”是与键“UUID”如何使用@OneToMany连接两个表,而让外键或连接表

CREATE TABLE Report(
    Id BIGINT AUTO_INCREMENT PRIMARY KEY, 
    Uuid CHAR(32) BINARY NOT NULL UNIQUE 
) 

CREATE TABLE ReportContent(
    Id BIGINT AUTO_INCREMENT PRIMARY KEY, 
    Uuid CHAR(32) BINARY NOT NULL, 
    Type INT NOT NULL 
) 
ALTER TABLE (ReportContent) ADD UNIQUE (Uuid, Type); 

public class Report { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "Id") 
    private long id; 
    @Column(name = "Uuid", columnDefinition = "char(32)", nullable = false, unique = true, updatable = false) 
    private String uuid; 
    @OneToMany 
    @JoinColumn(name = "Uuid") 
    private List<ReportContent> contents; 

    // setter and getter 
} 

public class ReportContent { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "Id") 
    private long id; 
    @Column(name = "Uuid", nullable = false, columnDefinition = "char(32)", updatable = false) 
    private String uuid; 

    // setter and getter 
} 

我怎么办相关,选择报告和休眠发送SQL获取ReportContents与条件Report.Uuid = ReportContent.Uuid?

回答

0

尝试像

public class Report { 

    // ... 

    @OneToMany(mappedBy = "report") 
    private List<ReportContent> contents; 

    // ... 
} 

而且

public class ReportContent { 

    // ... 

    @ManyToOne 
    @JoinColumn(name = "Uuid") 
    private Report report; 

    // ... 
} 
+0

遗憾,它没有作用。 – Robinson