2014-09-26 186 views
-2

需要幫助解決一些Hibernate問題。hibernate - 如何一次堅持所有實體和子實體

1)想要一次運行create()方法,從一個對象中堅持所有實體和子實體。可能嗎?

2)連接表有問題。沒有什麼是堅持下去的。我試圖將@ManyToMany映射設置爲PersonAddress類中的集合。從未使它工作並填充數據庫中的PERSON_ADDRESS表。

我有兩種測試方法,我嘗試從我的測試用例類運行。他們都在我的git。方法是testJoinTable()testLoadEverything()。請看一下。 Hibernate映射來自模型包中的類。

Address.java

@Entity 
@Table(name = "ADDRESS") 
public class Address implements Serializable { 

    @ManyToOne(targetEntity = Country.class) 
    @JoinColumn(name = "COUNTRY_ID", referencedColumnName = "COUNTRY_ID", insertable = true, nullable = true, unique = false, updatable = true) 
    private Country country; 

    @ManyToMany(mappedBy = "address") 
    private Set<Person> persons = new HashSet<Person>(); 

    @Column(name = "STREET", length = 100) 
    private String street; 

    @Column(name = "POST_CODE", length = 50) 
    private String postCode; 

    @Column(name = "ADDRESS_TYPE", length = 50) 
    private String addressType; 

    @Column(name = "ADDRESS_ID", nullable = false, precision = 20) 
    @Id 
    @SequenceGenerator(name = "AddressSeq", sequenceName = "ADDRESS_SEQ", allocationSize = 1) 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AddressSeq") 
    private Long addressId; 

    @Column(name = "CITY", length = 100) 
    private String city; 

    @Column(name = "CREATED_ON", nullable = false) 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date createdOn; 

    @Column(name = "MODIFIED_ON") 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date modifiedOn; 

    @Column(name = "DISTRICT_NAME", length = 100) 
    private String districtName; 

    @OneToMany (fetch = FetchType.LAZY, targetEntity = SomeId.class, mappedBy = "address") 
    @Cascade({CascadeType.SAVE_UPDATE, CascadeType.ALL}) 
    private Set<SomeId> someIds; 

} 

Person.java

@Entity 
@Table(name = "PERSON") 
public class Person implements Serializable { 

    @Column(name = "PERSON_ID", nullable = false, precision = 20) 
    @Id 
    private Long personId; 

    @ManyToMany 
    @JoinTable(name="PERSON_ADDRESS", 
       joinColumns={@JoinColumn(name="PERSON_ID")}, 
       inverseJoinColumns={@JoinColumn(name="ADDRESS_ID")}) 
    private Set<Address> address = new HashSet<Address>(); 

    @Column(name = "CREATED_ON", nullable = false) 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date createdOn; 

    @Column(name = "MODIFIED_ON") 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date modifiedOn; 
} 

Country.java

@Entity 
@Table(name = "COUNTRY") 
@NamedQuery(name = "findCountryByCode", query = "from Country where code like :code") 
public class Country implements Serializable { 

    @Column(name = "COUNTRY_ID", nullable = false, precision = 20) 
    @Id 
    @SequenceGenerator(name = "CountrySeq", sequenceName = "COUNTRY_SEQ", allocationSize = 1) 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CountrySeq") 
    private Long countryId; 

    @OneToMany(targetEntity = Birth.class, mappedBy = "country") 
    private Collection<Birth> birthCollection; 

    @OneToMany(targetEntity = Address.class, mappedBy = "country") 
    private Collection<Address> addressCollection; 

    @Column(name = "CODE", nullable = false, length = 2) 
    private String code; 

    @Column(name = "CREATED_ON", nullable = false) 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date createdOn; 

    @Column(name = "MODIFIED_ON") 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date modifiedOn; 
} 

SomeId.java

@Entity 
@Table(name = "SOME_ID") 
public class SomeId { 

    @Column(name = "SOME_ID_ID", unique = true, nullable = false, precision = 20, scale = 0) 
    @Id 
    @SequenceGenerator(name = "SomeIdSeq", sequenceName = "SOME_ID_ID_SEQ", allocationSize = 1) 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SomeIdSeq") 
    private Long someIdId; 

    @ManyToOne(optional = false, targetEntity = Address.class, fetch = FetchType.EAGER) 
    @JoinColumn(name = "ADDRESS_ID", referencedColumnName = "ADDRESS_ID", insertable = true, nullable = true, unique = false, updatable = true) 
    private Address address; 

    @Column(name = "CREATED_ON", nullable = false) 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date createdOn; 

    @Column(name = "MODIFIED_ON") 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date modifiedOn; 

    @Column(name = "SOME_ID", nullable = false, length = 100) 
    private String someId; 
} 

爲了更容易得到什麼我試圖完成這一點,我會在這裏貼上我的測試方法:像你需要指定從人多對多級聯選項

@Test 
public void testJoinTable() { 
    addressDao.create(address1); 
    addressDao.create(address2); 
    Address address = addressDao.get(address2.getAddressId()); 
    assertTrue("address2 don't have 3 persons as expected", address.getPersons().size() == 3); 
    long numberOfRecordsInJoinTable = personAddressDao.count(); 
    assertTrue("join table PERSON_ADDRESS is not used by Hibernate", numberOfRecordsInJoinTable > 0); 
} 

@Test 
public void testLoadEverything() { 
    address1.setSomeIds(new HashSet<SomeId>()); 
    address1.getSomeIds().add(someId1); 
    address1.getSomeIds().add(someId2); 
    addressDao.create(address1); 
    long numberOfRecordsInSomeIdTable = someIdDao.count(); 
    assertTrue("someIds are not persisted", numberOfRecordsInSomeIdTable > 0); 
} 
+1

您需要顯示您的實體Person,Address,Country,以便我們可以看到註釋或XML。 – isah 2014-09-26 17:07:07

+0

你的問題不清楚 – Chaitanya 2014-09-26 17:13:09

+0

對不起,我粘貼了那個git repo鏈接,希望能更容易地看到解決我的問題所需的一切。我使用代碼示例編輯了我的問題以及有關我的問題的更多信息。 – shx 2014-09-26 19:16:33

回答