如何使用Hibernate保存复杂对象?

时间:2014-03-08 00:50:12

标签: java hibernate

我有三张桌子:

CREATE TABLE catalog (
    id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
    type_id INT,
    genre_id INT,
    product_name VARCHAR(100),
    FOREIGN KEY ( genre_id ) REFERENCES genres ( genre_id ),
    FOREIGN KEY ( type_id ) REFERENCES types ( type_id )
);

CREATE TABLE genres (
    genre_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
    genre_name VARCHAR(50)
);

CREATE TABLE types (
    type_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
    type_name VARCHAR(50)
);

我也有Java类

@Entity
@Table(name = "catalog", catalog = "media_store_db")
public class Catalog implements Serializable {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "product_name", length = 100)
    private String productName;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "genre_id", referencedColumnName = "genre_id")
    private Genre genre;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "type_id", referencedColumnName = "type_id")
    private Type type;


@Entity
@Table(name = "genres", catalog = "media_store_db")
public class Genre implements Serializable {

    @Id
    @Column(name = "genre_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "genre_name")
    private String name;

@Entity
@Table(name = "types", catalog = "media_store_db")
public class Type implements Serializable {

    @Id
    @Column(name = "type_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "type_name")
    private String name;

是否可以保存(使用Hibernate Session的 save()方法)这样的目录对象

Catalog catalog = new Catalog();
catalog.setProductName("Product");
catalog.setGenre(new Genre());
catalog.setType(new Type());
save(catalog);

没有编写SQL?我需要做什么类型和类型?我应该设置两个实例的ID吗?

UPD:

此代码可以正常使用

Catalog catalog = new Catalog();
catalog.setProductName("12 Years a Slave");
catalog.setGenre(genreRepository.get(Long.valueOf(1)));
catalog.setType(typeRepository.get(Long.valueOf(1)));
Session session = cfg.getSession();
Transaction tx = session.beginTransaction();
session.save(catalog);
tx.commit();
session.close();

1 个答案:

答案 0 :(得分:0)

当然,您可以使用persist(Object obj)将生成的Object保留在数据库中。 那么,您应该在JUnit测试中测试该函数。在商业代码中,它应该做你的DAO。 不,生成了所有ID,您不需要设置ID。它由Hibernate管理。 对于您的示例,UnitTest应如下所示:

public class DataGenerationTest {

private EntityManager em;

@Before 
public void init(){
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
    em = emf.createEntityManager();
}

@Test
public void shouldAddSomeCatalogs(){
em.getTransaction().begin();

Catalog catalog = new Catalog();
catalog.setProductName("Proguct");
catalog.setGenre(new Genre());
catalog.setType(new Type());
em.persist(catalog);
    em.getTransaction().commit();
    em.close();
}
}

(当然你必须从EntityManagerFactory重命名PersistenceUnit测试。它应该与persistence.xml中的命名PersistenceUnit匹配)

其他有趣的讲座:

Hiberante Session Doc

Small example (GitHub)