有关hibernate的复合模式的帮助

时间:2010-04-08 21:58:34

标签: java database hibernate gwt annotations

所以我被困了,我正在创建一个gwt web应用程序,我将使用树(gwt Tree和TreeItems)结构来显示文件夹列表(类Folder)和文件(类FileLocation),文件夹和文件位置class将基于复合模式的类实现一个Hierarchy接口。但我使用hibernate来存储我的数据,我正在使用注释将数据映射到数据库。我的麻烦是我不知道如何注释我的界面。

让你们中的任何人使用复合模式,同时使用hibernate持久保存数据

public interface Hierarchy(){
// a few abstract methods that will be implemented by the sub classes
 }


@Entity
@Table()
public class Folder implements Serializable, Hierarchy {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "folder_id", updatable = false, nullable = false)
private int id;
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinTable(name = "FOLDER_FILELOCATION", joinColumns = { 
        @JoinColumn(name = "folder_id") }, inverseJoinColumns = { 
        @JoinColumn(name = "file_information_id") })
private List<Hierarchy> children = new ArrayList<Hierarchy>() ;
@Column(name = "folder_name")
private String folderName;
@Column(name = "tree_item")
private TreeItem item;
@Column (name = "parent")
private Hierarchy parent;



@Entity
@Table(name = "FILE_INFORMATION_TABLE")
public class FileInformation implements Serializable, Hierarchy {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "file_information_id", updatable = false, nullable = false)
private int fiId;
@Column (name = "location")
private String location;
@Column(name = "tree_item")
private TreeItem item;
@Column (name = "parent")
    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
    @JoinTable(name="FOLDER_FILELOCATION",
    joinColumns = @JoinColumn(name="filelocation_id"),
    inverseJoinColumns = @JoinColumn(name="folder_ID"))  
private Hierarchy parent;

3 个答案:

答案 0 :(得分:1)

浏览一下hibernate文档和This Book后,我找到了解决问题的方法。我现在无法使用JPA注释的界面。所以我使用了一个带有支持继承的映射的抽象类。在此示例中,我使用单个表来存储所有值。但是我会考虑分开它们。

@Entity
@Table(name ="HIERARCHY")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(
    name = "HIERARCHY_TYPE", discriminatorType = DiscriminatorType.STRING)      
public abstract class  Hierarchy implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "hierarchy_id", updatable = false, nullable = false)
private int hId;



@Entity
@DiscriminatorValue("F")
public class Folder extends Hierarchy  {

@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinTable(name = "FOLDER_JOIN_FILELOCATION", joinColumns = { 
        @JoinColumn(name = "folder_id") }, inverseJoinColumns = { 
        @JoinColumn(name = "file_information_id") })
private List<Hierarchy> children = new ArrayList<Hierarchy>() ;
@Column(name = "folder_name")
private String folderName;
//@Column(name = "tree_item")
//private TreeItem item;
@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
   @JoinTable(name="FOLDER_JOIN_FOLDER",
        joinColumns = @JoinColumn(name="parent_folder_id"),
        inverseJoinColumns = @JoinColumn(name="folder_ID")
    ) 
private Hierarchy parent;




@Entity
@DiscriminatorValue("FI")
public class FileInformation extends Hierarchy  {


@Column (name = "location")
private String location;
//@Column(name = "tree_item")
//private TreeItem item;
@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
   @JoinTable(name="FILEINFORMATION_JOIN_FOLDER",
        joinColumns = @JoinColumn(name="filelocation_id"),
        inverseJoinColumns = @JoinColumn(name="folder_ID")
    )  
private Hierarchy parent;

答案 1 :(得分:0)

Composite是非常基本的hibernate映射,应该不是问题 但我不明白Mapping。我假设FOLDER_FILELOCATION是Folder和FileInformation之间的LinkTable。如果是这样,你实际上没有OneToMany而是ManyToMany Relation。但是你可以将它映射为OneToMany 你面临的问题是什么? 要获得更精确的答案,请花更多时间来解释您的具体问题。格式化给定代码也有帮助,但我可以尝试一下。

我不完全确定,但我认为根据模型,我认为你应该尝试这个:

public interface Hierarchy(){
// a few abstract methods that will be implemented by the sub classes
}


@Entity
@Table(name = "FOLDER_TABLE")
public class Folder  implements Hierarchy, Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "folder_id", updatable = false, nullable = false)
    private int id;

    @OneToMany
    @JoinTable(
            name="FOLDER_FILELOCATION",
            joinColumns = @JoinColumn( name="folder_id"),
            inverseJoinColumns = @JoinColumn( name="filelocation_id")
    )   
    private List<Hierarchy> children = new ArrayList<Hierarchy>() ;

    @Column(name = "folder_name")
    private String folderName;

    @Column(name = "tree_item")
    private TreeItem item;

    @Column (name = "parent")
    private Hierarchy parent;
}

@Entity
@Table(name = "FILE_INFORMATION_TABLE")
public class FileInformation implements Hierarchy, Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "file_information_id", updatable = false, nullable = false)
    private int fiId;

    @Column (name = "location")
    private String location;
    @Column(name = "tree_item")
    private TreeItem item;

    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
    @JoinTable(name="FOLDER_FILELOCATION",
        joinColumns = @JoinColumn(name="filelocation_id"),
        inverseJoinColumns = @JoinColumn(name="folder_ID")
    )   
    private Hierarchy parent;
}

希望这会有所帮助。

答案 2 :(得分:0)

我不是百分之百确定你要做什么(你是{{​​1}}一个界面,extending缺少parent注释)但是,对我来说注释不支持知识,接口(它们不是JPA规范的一部分)。请参阅HHH-4413this discussion。您可能需要使用XML mappings代替此。