首先是对的吗?就像 1 个供应商有多个付款,供应商有 1 个身份,即他的信息。
我想插入供应商支付的款项。 我目前正在做的是 POST 一个包含以下元素的 json 文件。
{
"date":"2017-02-17",
"dueDate":"2018-02-17",
"payable":2000,
"paid":1000,
"supplierId":1
}
现在在 Controller 中,我正在读取 requestJson 提取供应商的 Id,然后找到其类的对象,然后将其传递给 Payment 以添加付款。这是正确的方法吗?
case "payment": {
logger.debug("The condition for the insertion of the payment.");
try {
logger.debug("Populating the request Map with the request to identify the type of .");
requestMap = mapper.readValue(requestBody, Map.class);
} catch (IOException e) {
e.printStackTrace();
}
logger.debug("Identifying the key for the payment is it for customer or supplier.");
if ((requestMap.containsKey("customerId")) || requestMap.containsKey("pending")){
logger.debug("The request for the adding payment of the customer has been received.");
Customer customer = customerRepository.findById(Integer.parseInt(requestMap.get("customerId")));
// Payment payment = new Payment(requestMap.get("dueDate"), requestMap.get("pending"), customer, requestMap.get("data"))
// paymentRepository.save()
} else if (requestMap.containsKey("supplierId") || requestMap.containsKey("outstanding")){
}
}
我这里有信息模型。
@Entity // This tells Hibernate to make a table out of this class
@Table(name = "Information")
public class Information {
private Integer id;
private String name;
private String contactNo;
private String email;
private String address;
private Customer customer;
private Supplier supplier;
public Information() {
}
public Information(String name, String contactNo, String email, String address) {
this.name = name;
this.contactNo = contactNo;
this.email = email;
this.address = address;
}
public Information(Customer customer) {
this.customer = customer;
}
public Information(Supplier supplier) {
this.supplier = supplier;
}
/**
*
* @return
*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="name", nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "contact_no", unique = true, nullable = false)
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
@Column(name = "email", unique = true, nullable = false)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@OneToOne(mappedBy = "information")
@JsonBackReference(value = "customer-reference")
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@OneToOne(mappedBy = "information")
@JsonBackReference(value = "supplier-reference")
public Supplier getSupplier() {
return supplier;
}
public void setSupplier(Supplier supplier) {
this.supplier = supplier;
}
@Override
public String toString() {
return "Information{" +
"id=" + id +
", name='" + name + '\'' +
", contactNo='" + contactNo + '\'' +
", email='" + email + '\'' +
", address='" + address + '\'' +
'}';
}
}
这里我有 Customer 还有一些与此相关的其他关系忽略它们我想明白这一点。
@Entity
@Table(name = "Customer") //maps the entity with the table. If no @Table is defined,
// the default value is used: the class name of the entity.
public class Customer {
private Integer id;
private Information information;
private Set<Payment> payments;
private Set<Sale> sales;
private Set<Orders> orders;
public Customer() {
}
public Customer(Information information) {
this.information = information;
}
/**
*
* @return
*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@OneToOne(cascade = CascadeType.ALL ,fetch = FetchType.EAGER)
@JoinColumn(name = "Information_id")
@JsonManagedReference(value = "customer-reference")
public Information getInformation() {
return information;
}
public void setInformation(Information information) {
this.information = information;
}
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
public Set<Payment> getPayments() {
return payments;
}
public void setPayments(Set<Payment> payments) {
this.payments = payments;
}
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "CustomerSales", joinColumns = @JoinColumn(name = "Customer_id",
referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "Sale_id", referencedColumnName = "id"))
public Set<Sale> getSales() {
return sales;
}
public void setSales(Set<Sale> sales) {
this.sales = sales;
}
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
public Set<Orders> getOrders() {
return orders;
}
public void setOrders(Set<Orders> orders) {
this.orders = orders;
}
@Override
public String toString() {
return String.format("Customer{id = %d, " +
"name = %s, " +
"contact_no = %s, " +
"address = %s, " +
"email = %s}",
id,information.getName(), information.getContactNo(), information.getAddress(), information.getEmail());
}
}
这是供应商模型。
@Entity
@Table(name = "Supplier")
public class Supplier {
private Integer id;
private Information information;
private Set<Payment> payments;
private Set<Orders> orders;
private Set<Purchase> purchases;
public Supplier() {
}
public Supplier(Information information) {
this.information = information;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "InformationId")
@JsonManagedReference(value = "supplier-reference")
public Information getInformation() {
return information;
}
public void setInformation(Information information) {
this.information = information;
}
@OneToMany(mappedBy = "supplier", cascade = CascadeType.ALL)
public Set<Payment> getPayments() {
return payments;
}
public void setPayments(Set<Payment> payments) {
this.payments = payments;
}
@OneToMany(mappedBy = "supplier", cascade = CascadeType.ALL)
public Set<Orders> getOrders() {
return orders;
}
public void setOrders(Set<Orders> orders) {
this.orders = orders;
}
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "SupplierPurchases", joinColumns = @JoinColumn(name = "Supplier_id",
referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "Purchase_id", referencedColumnName = "id"))
public Set<Purchase> getPurchases() {
return purchases;
}
public void setPurchases(Set<Purchase> purchases) {
this.purchases = purchases;
}
}
最后但并非最不重要的是我们有支付模型。
@Entity
@Table(name="Payment")
public class Payment {
private Integer id;
private Date dueDate;
private Long paid;// When you are in debit you have to pay to supplier
private Long payable; // When you have to take from customer.
private Date date;
private Customer customer;
private Supplier supplier;
private PaymentMethod paymentMethod;
public Payment() {
}
public Payment(Date dueDate, Long payable, Date date, Customer customer, PaymentMethod paymentMethod) {
this.dueDate = dueDate;
this.paid = payable;
this.date = date;
this.customer = customer;
this.paymentMethod = paymentMethod;
}
public Payment(Date dueDate, Long paid, Date date, Supplier supplier, PaymentMethod paymentMethod) {
this.dueDate = dueDate;
this.paid = paid;
this.date = date;
this.supplier = supplier;
this.paymentMethod = paymentMethod;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(nullable = false)
public Date getDueDate() {
return dueDate;
}
public void setDueDate(Date dueDate) {
this.dueDate = dueDate;
}
public Long getPaid() {
return paid;
}
public void setPaid(Long paid) {
this.paid = paid;
}
public Long getPayable() {
return payable;
}
public void setPayable(Long payable) {
this.payable = payable;
}
@ManyToOne
@JoinColumn(name = "CustomerId")//mappedBy indicates the entity is the inverse of the relationship.
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@ManyToOne
@JoinColumn(name = "SupplierId")
public Supplier getSupplier() {
return supplier;
}
public void setSupplier(Supplier supplier) {
this.supplier = supplier;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@ManyToOne
@JoinColumn(name = "PaymentMethodId")
public PaymentMethod getPaymentMethod() {
return paymentMethod;
}
public void setPaymentMethod(PaymentMethod paymentMethod) {
this.paymentMethod = paymentMethod;
}
@Override
public String toString() {
return "Payment{" +
"id=" + id +
", dueDate=" + dueDate +
", paid=" + paid +
", payable=" + payable +
", date=" + date +
'}';
}
或者是否有更好的方法来执行此任务?
感谢:)
最佳答案
I am reading the requestJson extracting the Id of the supplier and then finding the object of its class and then passing it to the Payment for adding the payments. Is this the right way ?
让 Spring 框架为您完成这项工作。先稍微修改一下JSON请求:
{
"date":"2017-02-17",
"dueDate":"2018-02-17",
"payable":2000,
"paid":1000,
"supplier":{"id":1}
}
我只能猜测您的 REST Controller 当前的样子,但示例实现可能如下所示:
@RestController
@RequestMapping("/payment")
public class PaymentController {
@Autowired
private final PaymentRepository paymentRepository;
public PaymentController(PaymentRepository paymentRepository) {
this.paymentRepository = paymentRepository;
}
@RequestMapping(method = RequestMethod.POST)
ResponseEntity<?> insert(@RequestBody Payment payment) {
paymentRepository.save(payment);
return ResponseEntity.accepted().build();
}
}
这种方法导致请求的主体直接映射到 Payment 对象以及自动实例化的 Supplier 字段,因此足以将其保存到存储库中。它易于维护且简洁。
显然这里没有考虑 Customer 和 PaymentMethod 关系,但您可以轻松扩展 JSON 请求以支持它们。
I have the following schema
...
First thing is it right ? Like 1 Supplier has multiple payments and supplier has 1 identity that is his Information.
总的来说,我认为您的架构没有错,但与往常一样,这取决于您想要实现什么。这绝对应该有效,但总有一些要点,例如性能和内存注意事项会受到数据建模方式的影响:
在这里您可以找到一种利用 JPA 继承的替代方法:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "ContractorType")
public class Contractor {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "contact_no", unique = true, nullable = false)
private String contactNo;
@Column(name = "email", unique = true, nullable = false)
private String email;
private String address;
}
上述实体替换了创建下表的Customer、Supplier 和Information 实体,但仅汇总了它们的公共(public)部分:
Contractor
----------
contractor_type <---- discriminator column for Customer and Supplier
id
name
contact_no
email
address
其余的特定字段(销售、采购、订单)需要放在派生实体中,比方说 CustomerOperations 和 SupplierOperations。他们像以前一样创建了两个连接表,所以这里没有任何变化。
@Entity
@DiscriminatorValue("C")
public class CustomerOperations extends Contractor {
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
private Set<Payment> payments;
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
private Set<Orders> orders;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "CustomerSales",
joinColumns = @JoinColumn(name = "Customer_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "Sale_id", referencedColumnName = "id"))
private Set<Sale> sales;
}
@Entity
@Table(name = "SupplierPurchases ")
@DiscriminatorValue("S")
public class SupplierPurchases extends Contractor {
@OneToMany(mappedBy = "supplier", cascade = CascadeType.ALL)
private Set<Payment> payments;
@OneToMany(mappedBy = "supplier", cascade = CascadeType.ALL)
private Set<Orders> orders;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "SupplierPurchases",
joinColumns = @JoinColumn(name = "Supplier_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "Purchase_id", referencedColumnName = "id"))
private Set<Purchase> purchases;
}
这种方法往往工作得更快,因为底层 SQL 需要更少的连接,因此执行计划可能更有效。
值得一提的是,JPA 提供了其他继承策略。选择合适的不是一件容易的事,因为它们各有优点和缺点。如果您对细微差别感兴趣,请仔细查看 here .
关于java - 如何在 Spring Boot 中使用 hibernate/jpa 将数据插入 mysql 关系表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46723936/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h