【问题标题】:Problem with add transaction to database: Error accessing field [private long main.Emails.id] by reflection for persistent property [main.Emails#id]将事务添加到数据库的问题:通过反射持久属性 [main.Emails#id] 访问字段 [private long main.Emails.id] 时出错
【发布时间】:2020-01-04 15:21:40
【问题描述】:

我无法将记录添加到我的数据库中,我有 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]

我看到休眠不能为电子邮件分配 ID 号,但为什么会这样?我写了 addnotation 并尝试使用 strategy = GenerationType.IDENTITY 但问题是一样的。谁能告诉我解决问题的方法?我用过 Hibernate 5.4.10 和 MySql Workbench

【问题讨论】:

    标签: java hibernate jpa


    【解决方案1】:

    试试@GeneratedValue(strategy=GenerationType.AUTO)

    javax.persistence.GenerationType.AUTO

    表示持久性提供者应该为特定数据库选择适当的策略。 AUTO 生成策略可能期望数据库资源存在,或者它可能会尝试创建一个。如果供应商不支持架构生成或无法在运行时创建架构资源,供应商可能会提供有关如何创建此类资源的文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-26
      • 2021-07-19
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多