2016-07-05 79 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