@Entity
@Table(name = "sale")
public class Sale implements Serializable {
private static final long serialVersionUID = -3857608619547245492L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "company", nullable = false)
private Long company;
@OneToMany(mappedBy = "sale", cascade = CascadeType.ALL, orphanRemoval = true)
private List<SaleItem> saleItems = new ArrayList<>();
public Sale() {
}
public void addSaleItem(SaleItem saleItem) {
saleItems.add(saleItem);
saleItem.setSale(this);
}
//****************************Getters and Setters****************************
}
@Entity
@Table(name = "sale_item")
public class SaleItem implements Serializable {
private static final long serialVersionUID = 1016354254590870341L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "company", nullable = false)
private Long company;
@Column(name = "description", nullable = false)
private String description;
@Column(name = "amount", nullable = false)
private Double amount;
@Column(name = "price", nullable = false)
private Double price;
@ManyToOne(fetch = FetchType.LAZY)
private Sale sale;
public SaleItem() {
}
//****************************Getters and Setters****************************
public void setSale(Sale sale) {
this.sale = sale;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof SaleItem))
return false;
return id != null && id.equals(((SaleItem) o).getId());
}
@Override
public int hashCode() {
return 31;
}
@Override
public String toString() {
return "SaleItem [id=" + id + ", company=" + company + ", product=" + product + ", created=" + created
+ ", updated=" + updated + ", description=" + description + ", amount=" + amount + ", price=" + price
+ ", discount=" + discount + ", user=" + user + ", unit=" + unit + ", sale=" + sale + "]";
}
}
//*********************** My Controller ***********************
/**
* Create Sale
*
* @param sale
* @param result
* @return ResponseEntity<Response<SaleDto>>
* @throws ParseException
*/
@PostMapping
public ResponseEntity<Response<SaleDto>> create(@Valid @RequestBody SaleDto saleDto, BindingResult result)
throws ParseException {
log.info("Creating sale: {}", saleDto.toString());
Response<SaleDto> response = new Response<SaleDto>();
validateSale(saleDto, result);
if (result.hasErrors()) {
log.error("Error - invalid information for Sale: {}", result.getAllErrors());
result.getAllErrors().forEach(error -> response.getErrors().add(error.getDefaultMessage()));
return ResponseEntity.badRequest().body(response);
}
response.setData(this.convertSaleDto(this.nova(saleDto, result)));
return ResponseEntity.ok(response);
}
private Sale nova(SaleDto saleDto, BindingResult result) throws ParseException {
SaleItem saleItem = new SaleItem();
saleItem.setCompany(saleDto.getCompany());
saleItem.setPrice(saleDto.getItems().get(0).getPrice());
saleItem.setDescription(saleDto.getItems().get(0).getDescription());
saleItem.setUnit(saleDto.getItems().get(0).getUnit());
Sale sale = new Sale();
sale.setCompany(saleDto.getCompany());
sale.setUser(saleDto.getUser());
sale.addSaleItem(saleItem);
return this.saleService.persist(sale);
}
//**************************My Service**************
/**
* Persist a sale
*
* @param Sale
* @return Sale
*/
Sale persist(Sale sale);
//******************My impl***********************
@Transactional
public Sale persist(Sale sale) {
log.info("Persisting sale {}", sale);
return this.saleRepository.save(sale);
}
SLF4J: Failed toString() invocation on an object of type [com.pissolato.api.entitys.Sale]
Reported exception:
java.lang.StackOverflowError
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at java.lang.StringBuilder.<init>(Unknown Source)
at com.pissolato.api.entitys.SaleItem.toString(SaleItem.java:189)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at java.util.AbstractCollection.toString(Unknown Source)
at java.lang.String.valueOf(Unknown Source)
2019-12-27 02:46:40.041 INFO 2684 --- [nio-8080-exec-2] c.p.a.services.implementation.SaleImpl : Persisting sale [FAILED
toString()]hibernate:/*插入com.pissolato.api.entitys.sale*/插入到sale(company,created,description,discount,sub_value,type,updated,user,value)值(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
您在反向侧使用mappedby,因此在实体(saleItem)的所属侧不需要join列。您的“Sale Item”实体应该如下所示
@Entity
@Table(name = "sale_item")
public class SaleItem implements Serializable {
private static final long serialVersionUID = 1016354254590870341L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "company", nullable = false)
private Long company;
@Column(name = "description")
private String description;
@Column(name = "amount", nullable = false)
private Double amount;
@Column(name = "price", nullable = false)
private Double price;
@ManyToOne(fetch = FetchType.LAZY)
private Sale sale;
public SaleItem() {
}
public void setSale(Sale sale) {
this.sale = sale;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof SaleItem))
return false;
return id != null && id.equals(((SaleItem) o).getId());
}
@Override
public int hashCode() {
return 31;
}
}
下面是我为测试编写的示例服务类。
@Service
public class SaleService {
@Autowired
SaleRepository saleRepo;
@Transactional
public void saveSales() {
SaleItem saleItem = new SaleItem();
saleItem.setCompany(12L);
saleItem.setAmount(232.0);
saleItem.setPrice(23232.0);
Sale sale = new Sale();
sale.setCompany(12L);
\\ Utility class will update on either side of the entities
sale.addSaleItem(saleItem);
saleRepo.save(sale);
}
}
跟踪日志输出。
2019-12-26 21:33:32.980 DEBUG 12024 --- [nio-8080-exec-1] org.hibernate.SQL :
insert
into
sale
(company, id)
values
(?, ?)
2019-12-26 21:33:32.985 TRACE 12024 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [12]
2019-12-26 21:33:32.985 TRACE 12024 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [BIGINT] - [1]
2019-12-26 21:33:33.069 DEBUG 12024 --- [nio-8080-exec-1] org.hibernate.SQL :
insert
into
sale_item
(amount, company, description, price, sale_id, id)
values
(?, ?, ?, ?, ?, ?)
2019-12-26 21:33:33.070 TRACE 12024 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [DOUBLE] - [232.0]
2019-12-26 21:33:33.070 TRACE 12024 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [BIGINT] - [12]
2019-12-26 21:33:33.071 TRACE 12024 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [3] as [VARCHAR] - [null]
2019-12-26 21:33:33.071 TRACE 12024 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [4] as [DOUBLE] - [23232.0]
2019-12-26 21:33:33.071 TRACE 12024 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [5] as [BIGINT] - [1]
2019-12-26 21:33:33.071 TRACE 12024 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [6] as [BIGINT] - [2]
我们必须在可选元素“mappedby”中指定什么。是特定模型的table_name还是classname? 哪个是正确的? 这个
问题内容: 我目前正在阅读有关实体关联的Hibernate文档,但遇到一些困难却难以理解。它在本质上做的区别和联系。尽管我在实际项目中使用了它们,但是我无法完全理解它们之间的差异。据我了解,如果一个表/一个实体与另一个实体有关联,则该关联应来自另一侧。那么,我们应该如何根据具体情况决定选择哪个呢?它又如何影响数据库/查询/结果?到处都有很好的例子吗? PS:我认为这与问题相关,如果有人可以解释关联
我对冬眠学习还不熟悉,所以研究冬眠学习中使用的关系。我所了解的是-对于带有外键的一域单向关系映射,连接列将位于目标实体中(在我的示例中为Review类)。我不需要让它双向运行。 但是在实现它的同时,我得到了以下错误: OneToMany-单向-示例3 请帮帮忙!!!
所以我在网上搜索了我的问题的答案,但没有找到有帮助的东西,基本上是需要在两个类之间有一个ManyToOne关系,其中一个类有一个EmbeddedId,我要把代码留在这里,错误信息是它给(我使用野蝇来运行服务器)。 公共类InventoryPK实现可序列化{ } @实体@Table(name=“inventario”,schema=“mxnextmob”) 公共类库存扩展基本模型{ } 公共类公司扩
我在将OneToMany外键映射到复合主键时遇到问题。我已经尝试过很多解决方案,包括这个post@onetomany和复合主键?。 所以情况是: 因此,在Box实体中,我试图在Box和Color实体之间创建一个OneToMany映射。为此,我应该使用Color实体的复合主键吗?如果我试图将列连接到复合主键(就像我在Box实体中所做的那样),我确实会得到一个错误-无法定位逻辑列“locale”。 我
如果在中只有一组,这将非常好地工作。ASSESSMENT_COMMENT只有两列: 它将完美地代表一对多的关系。 现在问题来了: 那么,1)这是Hibernate中已知的bug吗?2)有办法解决这个问题吗?我可以强制Hibernate创建两个映射表,每个映射表一个吗?请记住,我不能更改类来引用(业务逻辑需求)