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表

这样做不规范化,可能会引入一系列其他问题.因人而异

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值