开发者

Create a JPA many-to-many relation using an id instead of an object

I have a large dataset which I am importing in bulk with rows looking like

(news_id, category_id_1, category_id_2,开发者_如何转开发 ..., category_id_9)

Each category_id_x is an integer from a fixed set of categories.

I want to map these multiple categories into a m2m relation for quicker searching.

I have a News table and a Category table.

The Category table maps category ids to category names.

I already have all the mappings setup in JPA for the various News fields, and I want to reuse this code.

My question is how can I import these m2m relations in JPA.

I was thinking along the lines of the following but I get errors saying that you can't create Category objects manually.

// News object
@Entity
@Table(name = "news", schema = "public", uniqueConstraints = {})
public class News implements java.io.Serializable {

    // Fields
    @Column(name = "asx_code")
    private String asxCode;
    @Id
    @Column(name = "annnum", unique = true, nullable = false)
    private String annnum;
    @Column(name = "company_name")
    private String companyName;
    ...
    ...
    @ManyToMany() 
    @JoinTable(name="announcement_types", 
        joinColumns= @JoinColumn(name="annnum"),
        inverseJoinColumns=@JoinColumn(name="report_type"))
    private Collection<ReportType> reportType;

function m(String) {
    // extract and return data from datasource
}

// Create a news object from 9 different report types
News o = new News();        
java.util.Collection<ReportType> types = new HashSet(); 
types.add(new ReportType(toInt(m("RepType0"))));
types.add(new ReportType(toInt(m("RepType1"))));
types.add(new ReportType(toInt(m("RepType2"))));
types.add(new ReportType(toInt(m("RepType3"))));
types.add(new ReportType(toInt(m("RepType4"))));
types.add(new ReportType(toInt(m("RepType5"))));
types.add(new ReportType(toInt(m("RepType6"))));
types.add(new ReportType(toInt(m("RepType7"))));
types.add(new ReportType(toInt(m("RepType8"))));
types.add(new ReportType(toInt(m("RepType9"))));
o.setReportType(types);

// Query
try {
    EntityTransaction entr = em.getTransaction();
    entr.begin();           
    em.persist(row); # row is our News object
    entr.commit();
} catch (Exception e) {
    e.printStackTrace();
    return false;
}
return true;

// ERROR
SEVERE: Could not synchronize database state with session
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: edu.unsw.eventstudy.shared.dto.ReportType

EDIT:

Seems I am dealing with the concept of detached entities. This is solved in Hibernate using the unsaved-value mapping or isSaved property. Now looking for a solution in JPA.


I only see the association in 1 direction. Maybe you forgot to do the other one, like: reportType0.add(news); reportType1.add(news); ... reportTypeN.add(news);

Also, you are "creating" instances of the ReportType instead of gathering them from the database. Do they already exist in the database?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜