怎么用java跟数据库建立关系,java – 关系和构建数据库
請嘗試以下方法:
我認為它涵蓋了你所有的設計要點.
我試圖在您的評論之間進行閱讀,我認為您希望實現一個系統,您可以在其中捕獲一些“規則”以供審核(我猜測,但示例可能是評論可以達到n在評論獲得一定程度的質量之前,必須至少有m個顧客評論.如果確實如此,我創建了一個ReviewTemplate類:
> ReviewTemplate將為您需要的每個值提供屬性/列.這些屬性/列在Review上重復
>使用多個行填充ReviewTemplate,然后在Course中創建一行并將其鏈接到一個ReviewTemplate
>當課程需要審核時,將ReviewTemplate中的字段復制到Review中
>在Java中,使用復制的值實現Review的業務規則 – 而不是ReviewTemplate上的值.
為什么要復制這些值?好吧,我敢打賭,在某些時候,用戶想要編輯ReviewTemplate表.如果是這樣,使用編輯過的ReviewTemplates對Review對象會發生什么? ReviewTemplate上的修改值是否會以某種方式使過去的評論失效并破壞您的業務邏輯?不,因為您將規則值復制到Review,因此過去的評論不會更改.
編輯:具體問題的答案
How do you see the duplicating? I can create an entity ReviewTemplate with the specified attributes. In this entity there will be a relationship with reviewlines and feedbackscores.
我將每個ReviewTemplate視為包含特定“類型”Review的原型值,其中可能包含默認reviewLine(但可能沒有意義)和默認feedbackScore.創建Review時,您將執行以下操作:
>實例化Review并使用ReviewTemplate中的值填充
>根據需要實例化盡可能多的CustomerReview對象,將它們鏈接到相關的Customer對象(我從之前的評論中推斷出這一步.在客戶自愿選擇審閱課程之前,省略此步驟也是有意義的)
>(如果適用)使用ReviewTemplate的默認值填充CustomerReview屬性feedbackScore
>根據需要實例化CustomerReviewLine記錄
如果您遵循此方法,則無需在ReviewTemplate和CustomerReviewLines之間添加關系.
When I e.g. state that customers 1 to 4 need to fill in the review 4 specific “objects” need to be created that will hold the information and also 4 sets of the needed reviewlines and feedbackscores need to be created so they all can hold the information.
絕對.
I just don’t know how to implement this is a JPA structure so the information is hold in the db … ?
JPA允許您以多種方式解決問題,但最佳做法是手動創建數據庫模式和Java類(例如,參見https://stackoverflow.com/a/2585763/1395668).因此,對于圖中的每個實體,您需要:
>編寫SQL DDL語句以創建表,列,主鍵和外鍵,以及
>編寫用@entity注釋表示的Java類.在類中,您還需要使用@id注釋id(主鍵)以及與@OneToMany或@ManyToOne的關系(注釋中的其他參數也要設置).
現在,在JPA方面,您可以執行以下操作:
ReviewTemplate template = course.getReviewTemplate(); //assuming the variable course
Review review = new Review();
review.setCourse(course);
review.setRuleOne(template.getRuleOne());
// Copy other properties here
EntityManager em = // get the entity manager here
em.persist(review);
// Assume a set or list of customers
for (Customer customer : customers) {
CustomerReview cr = new CustomerReview();
cr.setReview(review);
cr.setCustomer(customer);
cr.setFeedbackScore(template.getDefaultFeedbackScore());
// set other CustomerReview properties here
em.persist(cr);
// You can create CustomerReviewLine here as well
如果在標準EJB會話Bean中編寫,這將很好地進行交易,并且您將所有新記錄提交到數據庫中.
編輯2:補充問題
(我假設第二條評論完全取代第一條評論)
So when I create a reviewtemplate and I link it to a bunch of customers I write the template to the db and create a bunch of reviews based on the template but linked to the specific customer and with his own unique reviewlines and feedbackscores. Like I see it now the reviewline (more a question or discription) is the same for each review (of a template), it is only the score that changes between the customers
我終于想到了解了ReviewLine.我認為這是客戶輸入包含CustomerReview的文本行的地方.我現在相信ReviewLine是一個特定的問題,客戶被問及客戶提供的反饋評分.
根據這種理解,這是一個更新的ER /類圖.
請注意,有一些重大更改 – 還有幾個表:
> ReviewLineTemplate提供了一個存儲在ReviewTemplate上的模板問題的位置
>當實例化/插入評論(這是特定ReviewTemplate的副本)時,ReviewLineTemplates將復制為ReviewLines.復制操作允許兩個重要功能:
>在創建時,可以自定義Review及其ReviewLines,而不會影響ReviewTemplate或ReviewLineTemplate
>隨著時間的推移,ReviewTemplate和ReviewLineTemplate可以更新,編輯和不斷改進,而無需更改客戶已經回答的問題.如果CustomerFeedbackScore直接鏈接到ReviewLineTemplate,那么編輯ReviewLineTemplate將改變客戶已回答的問題,默默地使feedbackScore無效.
> FeedbackScore已移至ReviewLine和CustomerReview之間的聯接表.
請注意,此模型是完全非規范化的,這使得它更“正確”,但更難為其構建GUI.一個常見的’優化’可能是介紹:
ReviewTemplate和Review上的10個(比方說)列稱為reviewLine1到reviewLine10.
CustomerReview上的10個(比方說)列通過feedbackScore10調用feedbackScore1.
>刪除ReviewTemplateLine,ReviewLine和CustomerReviewLine表
這樣做不規范化,可能會引入一系列其他問題.因人而異
總結
以上是生活随笔為你收集整理的怎么用java跟数据库建立关系,java – 关系和构建数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 倒三角
- 下一篇: jupyter notebook和pyt