我是hibernate的新手,需要使用一对多和多对一的关系.它是我对象中的双向关系,因此我可以从任一方向遍历.mappedBy是推荐的方法,但是,我无法理解.有人能解释一下:
为了我的例子,这里是我的带注释的类:
Airline OWNS很多 AirlineFlightsAirlineFlights属于ONE Airline航空公司:
@Entity
@Table(name="Airline")
public class Airline {
private Integer idAirline;
private String name;
private String code;
private String aliasName;
private Set<AirlineFlight> airlineFlights = new HashSet<AirlineFlight>(0);
public Airline(){}
public Airline(String name, String code, String aliasName, Set<AirlineFlight> flights) {
setName(name);
setCode(code);
setAliasName(aliasName);
setAirlineFlights(flights);
}
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="IDAIRLINE", nullable=false)
public Integer getIdAirline() {
return idAirline;
}
private void setIdAirline(Integer idAirline) {
this.idAirline = idAirline;
}
@Column(name="NAME", nullable=false)
public …Run Code Online (Sandbox Code Playgroud) 拥有方到底意味着什么?一些映射示例(一对多,一对一,多对一)的解释是什么?
以下文本摘自Java EE 6文档中对@OneToOne的描述.你可以看到这个概念拥有方在里面.
定义与具有一对一多重性的另一个实体的单值关联.通常不必明确指定关联的目标实体,因为它通常可以从被引用的对象的类型推断出来.如果关系是双向的,则非拥有方必须使用OneToOne批注的mappedBy元素来指定拥有方的关系字段或属性.
我在JPA中的实体映射存在以下问题.我有两个实体,第一个是Lookup,第二个是Text,它代表实体的翻译.现在我需要将Lookup绑定到Text但我不希望Text引用Lookup.为了使这更复杂,Text不会在此关系中使用其主键,而是在TXTHEAD_CODE列中定义的元代码 .
Lookup.java
@Entity
@Table(name = "DATREG")
public class Lookup implements PersistableEntity {
@Id
@Column(name = "DATREG_META_CODE")
private String metaCode;
@OneToMany
@JoinTable(name="TXT",
joinColumns=@JoinColumn(name="DATREG_META_CODE", referencedColumnName="TXTHEAD_CODE"),
inverseJoinColumns=@JoinColumn(name="DATREG_META_CODE"))
private List<Text> text;
Run Code Online (Sandbox Code Playgroud)
Text.java
@Entity
@Table(name = "TXT")
public class Text {
@Id
@Column(name = "TXT_ID")
private Long id;
@Column(name = "TXTHEAD_CODE")
private String code;
Run Code Online (Sandbox Code Playgroud)
所以我尝试了这个(以及其他一些变化),但没有结果.我也无法在数据库中创建连接表,我不希望绑定查找到我的Text类.那么有人可以告诉我是否有其他方式?
在我的应用程序中,我使用JPA 2.0和Hibernate作为持久性提供程序.我在两个实体之间有一对多的关系(使用@JoinColumn和不使用@JoinTable).我想知道如何在JPA注释中指定inverse=true(如指定hbm.xml)来反转关系所有者.
谢谢.
我正在尝试IntelliJ IDEA,它警告我一个Hibernate协会,我不太明白.
一边:
@Entity
@Table(name = "MY_REQ_ASSIGNEE")
public class MyRequestAssignee extends BaseUser {
//...
@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, mappedBy = "myRequestAssignee")
private Collection<MyRequest> myRequests = new ArrayList<>();
//...
}
Run Code Online (Sandbox Code Playgroud)
多方:
@Entity
@Table(name = "MY_REQUEST")
public class MyRequest implements Persistable {
//...
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="ASSIGNEE_ID")
private MyRequestAssignee myRequestAssignee;
//...
}
Run Code Online (Sandbox Code Playgroud)
(Persistable只是一个具有id确保Hibernate可以访问它的属性的接口.)
我看到MyRequestAssignee红色下划线的类型和消息显示'Many To One' attribute type should not be 'Persistence Entity'. 我的关系有问题吗?
我一直在阅读很多关于@JoinColumn的内容,但我仍然不了解它背后的想法.
病人表
CREATE TABLE patient (
patient_id BIGINT NOT NULL,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
PRIMARY KEY(patient_id));
Run Code Online (Sandbox Code Playgroud)
车辆表
CREATE TABLE vehicles (
patient_id BIGINT NOT NULL,
vehicle_id BIGINT NOT NULL,
vehicle_manufacturer VARCHAR(255),
PRIMARY KEY (vehicle_id),
CONSTRAINT patienthasmanyvehicle FOREIGN KEY(patient_id) REFERENCES patient(patient_id));
Run Code Online (Sandbox Code Playgroud)
患者类
@OneToMany(mappedBy = "patient")
private Collection<Vehicle> patientVehicles = new ArrayList<Vehicle>();
Run Code Online (Sandbox Code Playgroud)
车辆类
@ManyToOne
@JoinColumn(name="patient_id")
private Patient patient;
Run Code Online (Sandbox Code Playgroud)
我对车辆类如何部分感到困惑,之间的关系是什么
Vehicle Class ---- Entity
@JoinColumn(name="patient_id") ---- annotation
private Patient patient ----field
Run Code Online (Sandbox Code Playgroud)
是吗?该车辆实体有一个外键到患者实体命名patient_id …
我正在使用spring框架,Hibernate和JSON开发rest web app.请假设我有两个实体,如下所示:
BaseEntity.java
@MappedSuperclass
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property = "id" )
public abstract class BaseEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
public long getId() {
return id;
}
}
Run Code Online (Sandbox Code Playgroud)
University.java
public class University extends BaseEntity {
private String uniName;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER,orphanRemoval = true)
@JoinColumn(name = "university_id")
private List<Student> students=new ArrayList<>();
// setter an getter
}
Run Code Online (Sandbox Code Playgroud)
Student.java
public class Student extends BaseEntity{
private String stuName;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "university_id",updatable = false,insertable …Run Code Online (Sandbox Code Playgroud) 对于Mentor to Students的实体,我的关系低于1米.导师有复合主键,我在学生中用作外键
@Entity
public class Mentor implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private MentorPK id;
private String email;
@OneToMany(mappedBy="mentor")
private Set<Student> students;
public MentorPK getId() {
return id;
}
//getters and setters
}
@Embeddable
public class MentorPK implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String add;
//getters and setters
//override equals and hashcode
}
@Entity
public class Student implements Serializable{
private static final long serialVersionUID = 1L; …Run Code Online (Sandbox Code Playgroud) 我有一个JPA 2 Web应用程序(Struts 2,Hibernate 4仅作为JPA实现).
当前的要求是将(非id)数字顺序字段(仅对某些行填充)添加到现有实体.根据特定条件插入新行时,我需要将新字段设置为its highest value + 1或NULL.
例如:
ID NEW_FIELD DESCRIPTION
--------------------------------
1 1 bla bla
2 bla bla <--- unmatched: not needed here
3 bla bla <--- unmatched: not needed here
4 2 bla bla
5 3 bla bla
6 4 bla bla
7 bla bla <--- unmatched: not needed here
8 5 bla bla
9 bla bla <--- unmatched: not needed here
10 6 bla bla
Run Code Online (Sandbox Code Playgroud)
在旧的SQL中,它将是这样的:
INSERT INTO myTable ( …Run Code Online (Sandbox Code Playgroud) 我有几个单向JPA2失败案例@OnetoMany关系下面是代码片段
@Entity
@Table(name="CUSTOMER")
@Access(AccessType.FIELD)
public class Customer {
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
@JoinColumn(name="CUSTOMER_ID", referencedColumnName="CUSTOMER_ID")
private List<Address> customerAddresses;
....
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,它无法在服务器启动期间创建实体管理器工厂,并出现以下错误
DEBUG - Second pass for collection: xx.xxx.xxxxxxx.core.domainmodel.customerinfo.Customer.customerAddresses
DEBUG - Binding a OneToMany: xx.xxx.xxxxxxx.core.domainmodel.customerinfo.Customer.customerAddresses through a foreign key
DEBUG - Mapping collection: xx.xxx.xxxxxxx.core.domainmodel.customerinfo.Customer.customerAddresses -> CUSTOMER_ADDRESS
DEBUG - Unable to build entity manager factory
java.lang.NullPointerException: null
at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1456) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
Run Code Online (Sandbox Code Playgroud)
当我referencedColumnName从@JoinColumn注释中删除属性时,服务器启动都很好
但是当我试图坚持下面失败的实体时,Hibernate为失败生成了跟踪(CUSTOMER_ID is the name of the identity generated PK column in CUSTOMER table and FK in the CUSTOMER_ADDRESS table …
我正在尝试使用 Spring Data JPA 实现双向一对多关系。我已经创建了用于保存和获取数据的测试用例,并且映射中没有问题,并且数据都保存在两个表中。但是当我尝试通过点击 Post 请求来创建数据时,外键没有被保存。客户和电话之间的映射是双向的一对多。
一对多1应用程序
package com.jwt.onetomany;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OneToMany1Application {
public static void main(String[] args) {
SpringApplication.run(OneToMany1Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
演示控制器
package com.jwt.onetomany.controller;
import java.net.URI;
import java.util.List;
import org.apache.tomcat.jni.Poll;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import com.jwt.onetomany.entity.Customer;
import com.jwt.onetomany.repo.CustomerRepository;
@RestController
public class DemoController {
@Autowired
CustomerRepository customerRepository;
@GetMapping("/getall")
ResponseEntity<List<Customer>> getAllCustomers() {
Iterable<Customer> findAll = customerRepository.findAll();
List<Customer> customers …Run Code Online (Sandbox Code Playgroud) rest hibernate spring-data-jpa spring-restcontroller spring-rest
我是Ebean世界的新手,我在实体之间设置一些关系遇到了一些困难.
我基本上有两个班,User而且Car.
一个用户可以有几辆车(我猜OneToMany),一辆车可以属于一个用户(所以我猜OneToOne).
我如何链接这两个实体?这是我到目前为止所做的
用户
@Entity
public class User extends Model{
@Id
@GeneratedValue
public int id;
public String name;
@ManyToMany(cascade=CascadeType.ALL)
public List<Car> car = new ArrayList<Car>();
}
Run Code Online (Sandbox Code Playgroud)
汽车
@Entity
public class Car extends Model{
@Id
@GeneratedValue
public int id;
@OneToOne(cascade = CascadeType.ALL)
public User user;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
PersistenceException:models.User.car上的错误在[models.Car]中找不到mappedBy属性[users]
有人可以清楚地向我解释如何以正确的方式使用注释(非常糟糕的文档),并告诉我为什么会出现此错误?
java playframework ebean playframework-2.2 playframework-2.3
我试图将用户列表映射到位置对象,但我得到映射异常.这是因为数据库无法识别List对象?或者为什么我会得到这个例外?
这是我的用户类:
@Entity
@Table(name = "users")
public class NewUser extends BaseEntity{
private String login;
private String fullName;
private Location location;
private Department department;
private Role role;
private Long days;
private String team;
private Long managerId;
private String hiredDate;
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public Location getLocation() {
return location;
}
@ManyToOne(targetEntity = Location.class) …Run Code Online (Sandbox Code Playgroud)