我在向数据库添加记录时遇到问题,我有 3 个实体类:
客户:
@Entity
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String companyName;
private String nip;
@OneToMany
@JoinColumn(name = "client_id")
private List<Adress> adress ;
@OneToMany(mappedBy = "client")
private List<Phones> phones ;
@OneToMany(mappedBy = "client")
private List<Emails> emails;
@OneToMany(mappedBy = "client")
private List<ContactPersons> contactPersons ;
@ManyToMany(mappedBy = "client")
private List<TasksAndActivities> tasks ;
@ManyToMany(mappedBy = "client")
private List<Meetings> meetings;
@OneToOne
private ServiceContracts serviceContracts;
@OneToOne
private ImplementationAgreements agreements;
public static class Builder {
private String companyName = null;
private String nip = null;
private List<Adress> adress = new ArrayList<>();
private List<Phones> phones = new ArrayList<>();
private List<Emails> emails = new ArrayList<>();
private List<ContactPersons> contactPersons = new ArrayList<>();
ServiceContracts serviceContracts = new ServiceContracts();
private ImplementationAgreements agreements = new ImplementationAgreements();
public Builder companyName(String companyName) {
this.companyName = companyName;
return this;
}
public Builder nip(String nip) {
this.nip = nip;
return this;
}
public Builder adress(List<Adress> adress) {
this.adress = adress;
return this;
}
public Builder phones(List<Phones> phones) {
this.phones = phones;
return this;
}
public Builder emails(List<Emails> emails) {
this.emails = emails;
return this;
}
public Builder contactPersons(List<ContactPersons> contactPersons) {
this.contactPersons = contactPersons;
return this;
}
public Builder serviceContracts(ServiceContracts serviceContracts) {
this.serviceContracts = serviceContracts;
return this;
}
public Builder agreements(ImplementationAgreements serviceContracts) {
this.agreements = agreements;
return this;
}
public Client build() {
return new Client(this);
}
}
private Client(Builder builder) {
this.companyName = builder.companyName;
this.nip = builder.nip;
this.adress = builder.adress;
this.phones = builder.phones;
this.emails = builder.emails;
this.contactPersons = builder.contactPersons;
this.serviceContracts = builder.serviceContracts;
this.agreements = builder.agreements;
}
public Client() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getNip() {
return nip;
}
public void setNip(String nip) {
this.nip = nip;
}
public List<Adress> getAdress() {
return adress;
}
public void setAdress(List<Adress> adress) {
this.adress = adress;
}
public List<Phones> getPhones() {
return phones;
}
public void setPhones(List<Phones> phones) {
this.phones = phones;
}
public List<Emails> getEmails() {
return emails;
}
public void setEmails(List<Emails> emails) {
this.emails = emails;
}
public List<ContactPersons> getContactPersons() {
return contactPersons;
}
public void setContactPersons(List<ContactPersons> contactPersons) {
this.contactPersons = contactPersons;
}
public ServiceContracts getServiceContracts() {
return serviceContracts;
}
public void setServiceContracts(ServiceContracts serviceContracts) {
this.serviceContracts = serviceContracts;
}
public ImplementationAgreements getAgreements() {
return agreements;
}
public void setAgreements(ImplementationAgreements agreements) {
this.agreements = agreements;
}
}
电子邮件:
@Entity
public class Emails {
@Id
@GeneratedValue
@Column(name = "id", updatable = false, nullable = false)
private long id;
private String emailName;
@ManyToOne(targetEntity = Client.class)
@JoinColumn(name = "client_id")
private Client client;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getEmailName() {
return emailName;
}
public void setEmailName(String emailName) {
this.emailName = emailName;
}
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
}
联系方式:
@Entity
public class ContactPersons {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String firstName;
private String lastName;
@ManyToMany(mappedBy = "contactPersons")
List<TasksAndActivities> tasks = new ArrayList<>();
@ManyToMany(mappedBy = "contactPersons")
List<Meetings> meetings = new ArrayList<>();
@OneToOne(targetEntity = Phones.class)
@JoinColumn(name = "id_phones")
List<Phones> phones = new ArrayList<>();
@OneToOne(targetEntity = Emails.class)
@JoinColumn(name = "id_emails")
List<Emails> emails = new ArrayList<>();
@ManyToOne(targetEntity = Client.class)
@JoinColumn(name = "id_contact_persons")
private Client client;
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public List<TasksAndActivities> getTasks() {
return tasks;
}
public void setTasks(List<TasksAndActivities> tasks) {
this.tasks = tasks;
}
public List<Meetings> getMeetings() {
return meetings;
}
public void setMeetings(List<Meetings> meetings) {
this.meetings = meetings;
}
public List<Phones> getPhones() {
return phones;
}
public void setPhones(List<Phones> phones) {
this.phones = phones;
}
public List<Emails> getEmails() {
return emails;
}
public void setEmails(List<Emails> emails) {
this.emails = emails;
}
}
和发送我的交易的静态主类:
public class Main {
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("tutorialdb");
EntityManager entityManager = entityManagerFactory.createEntityManager();
Adress adressObject = new Adress();
adressObject.setCity("New York");
adressObject.setHomeNumber(23);
adressObject.setNameStreet("Boolwar");
adressObject.setNumberStreet(4);
adressObject.setPostaCode(2192);
List<Adress> adressArray = new ArrayList<>();
adressArray.add(adressObject);
Phones phonesObj = new Phones();
phonesObj.setPhoneNumber("12345678");
phonesObj.setType("Mobile");
List<Phones> phonesArray = new ArrayList<>();
phonesArray.add(phonesObj);
Emails email = new Emails();
email.setEmailName("office@bi.com");
List<Emails> emailsArray = new ArrayList<>();
emailsArray.add(email);
ContactPersons contactPersonObj = new ContactPersons();
contactPersonObj.setFirstName("John");
contactPersonObj.setLastName("Smith");
Phones phonePersonObj = new Phones();
phonePersonObj.setType("mobile");
phonePersonObj.setPhoneNumber("1233333333");
List <Phones> phonePersonList = new ArrayList<>();
phonePersonList.add(phonePersonObj);
contactPersonObj.setPhones(phonePersonList);
Emails emailPersonObj = new Emails();
emailPersonObj.setEmailName("john@bi.com");
List <Emails> emailsPersonList = new ArrayList<>();
emailsPersonList.add(emailPersonObj);
contactPersonObj.setEmails(emailsPersonList);
List <ContactPersons> contactPersonsList = new ArrayList<>();
contactPersonsList.add(contactPersonObj);
ServiceContracts contract = new ServiceContracts();
contract.setDateStart(java.sql.Date.valueOf("2017-11-15"));
contract.setDateEnd(java.sql.Date.valueOf("2018-11-15"));
contract.setHoursContract(160.00F);
contract.setContract(true);
ImplementationAgreements im = new ImplementationAgreements();
im.setDateStart(java.sql.Date.valueOf("2017-12-15"));
im.setDateEnd(java.sql.Date.valueOf("2017-12-31"));
im.setHoursContract(20.00F);
im.setContract(true);
Client client = new Client.Builder()
.companyName("CO Investory")
.nip("1234567890")
.adress(adressArray)
.phones(phonesArray)
.emails(emailsArray)
.contactPersons(contactPersonsList)
.serviceContracts(contract)
.agreements(im)
.build();
phonesObj.setClient(client);
email.setClient(client);
phonePersonObj.setClient(client);
emailPersonObj.setClient(client);
entityManager.getTransaction().begin();
entityManager.persist(client);
entityManager.persist(adressObject);
entityManager.persist(phonesObj);
entityManager.persist(email);
entityManager.persist(contactPersonObj);
entityManager.persist(phonePersonObj);
entityManager.persist(emailPersonObj);
entityManager.persist(contract);
entityManager.persist(im);
entityManager.getTransaction().commit();
entityManager.refresh(client);
entityManager.close();
entityManagerFactory.close();
}
}
当我尝试发送交易时出现错误:
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private long main.Emails.id] by reflection for persistent property [main.Emails#id] : [main.Emails@767f6ee7]
我发现 hibernate 无法为电子邮件分配 ID 号,但为什么会发生这种情况?我已经写了 addnotation 并尝试使用 Strategy = GenerationType.IDENTITY 但问题是相同的。谁能告诉我解决问题的方法?我使用 Hibernate 5.4.10 和 MySql Workbench
最佳答案
尝试@GenerateValue(strategy=GenerationType.AUTO)
。
javax.persistence.GenerationType.AUTO
Indicates that the persistence provider should pick anappropriate strategy for the particular database. The AUTO generation strategy may expect a databaseresource to exist, or it may attempt to create one. A vendormay provide documentation on how to create such resourcesin the event that it does not support schema generationor cannot create the schema resource at runtime.
关于java - 通过反射持久属性 [main.Emails#id] 将事务添加到数据库 : Error accessing field [private long main. Emails.id] 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59592074/