1

I have two persistence classes Person and Account as below:

Person Class

package org.proggence.persistence;

import static javax.persistence.GenerationType.IDENTITY;

import java.sql.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "person")
public class Person {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "person_id", unique = true, nullable = false)
    private Long person_id;

    @Column(name = "first_name", unique = false, nullable = true)
    private String  first_name = "";

    @Column(name = "middle_name", unique = false, nullable = true)
    private String middle_name = "";

    @Column(name = "last_name", unique = false, nullable = true)
    private String last_name = "";

    @Column(name = "cnic", unique = false, nullable = true)
    private String cnic;

    @Column(name = "dob", unique = false, nullable = true)
    private Date dob;

    @Column(name = "gender", unique = false, nullable = true)
    private String gender;

    @Column(name = "zipcode", unique = false, nullable = true)
    private String zipcode;

    @Column(name = "address1", unique = false, nullable = true)
    private String address1;

    @Column(name = "address2", unique = false, nullable = true)
    private String address2;

    @Column(name = "phone_no", unique = false, nullable = true)
    private String phone_no;

    @Column(name = "image", unique = false, nullable = true)
    private String image;

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "person", cascade = CascadeType.ALL)
    private Account account;

    public Person(){}

    public Long getPerson_id() {
        return person_id;
    }

    public void setPerson_id(Long person_id) {
        this.person_id = person_id;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getMiddle_name() {
        return middle_name;
    }

    public void setMiddle_name(String middle_name) {
        this.middle_name = middle_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getCnic() {
        return cnic;
    }

    public void setCnic(String cnic) {
        this.cnic = cnic;
    }

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getZipcode() {
        return zipcode;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }

    public String getAddress1() {
        return address1;
    }

    public void setAddress1(String address1) {
        this.address1 = address1;
    }

    public String getAddress2() {
        return address2;
    }

    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    public String getPhone_no() {
        return phone_no;
    }

    public void setPhone_no(String phone_no) {
        this.phone_no = phone_no;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }
}

And Account Class

package org.proggence.persistence;

import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity(name = "Account")
@Table(name="ACCOUNT")
public class Account {

    @Id
    @Column(name = "PERSON_ID", unique = true, nullable = false)
    private Long person_id;

    @Column( name = "USERNAME" )
    private String username;    

    @Column( name = "PASSWORD" )
    private String password;

    @Column( name = "EMAIL" )
    private String email;

    @Column( name = "STATUS" )
    private String status;

    @Column( name = "WORK_MODE" )
    private String work_mode;

    @Column( name = "LOGIN_TIME" )
    private Timestamp login_time;

    @Column( name = "PASS_RESET_CODE" )
    private String pass_reset_code;

    @OneToOne(fetch = FetchType.LAZY)
    @PrimaryKeyJoinColumn
    private Person person;

    public Account(){}

    public Long getPerson_id() {
        return person_id;
    }

    public void setPerson_id(Long person_id) {
        this.person_id = person_id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getWork_mode() {
        return work_mode;
    }

    public void setWork_mode(String work_mode) {
        this.work_mode = work_mode;
    }

    public Timestamp getLogin_time() {
        return login_time;
    }

    public void setLogin_time(Timestamp login_time) {
        this.login_time = login_time;
    }

    public String getPass_reset_code() {
        return pass_reset_code;
    }

    public void setPass_reset_code(String pass_reset_code) {
        this.pass_reset_code = pass_reset_code;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
}

Why my delete query is not deleting the record?

public static void main(String[] arguments){
    try{


        EntityManager em = HibernateUtil.getEntityManager();

        new PersonDao().getAll();

        em.getTransaction().begin();
        Account account = em.find(Account.class, Long.valueOf("2"));

        em.remove(account);
        em.getTransaction().commit();


    }catch(Exception e){
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}

Other queries like Add, Update and Read are working fine.

Second whenever i tried to debug the code, in every query it causes the following exception. enter image description here

My persistence.xml is as below:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

<persistence-unit name="jpaHospice" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>org.proggence.persistence.Person</class>
    <class>org.proggence.persistence.Account</class>
    <properties>

        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
        <!-- <property name="hibernate.hbm2ddl.auto" value="create-drop"/> -->

        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hospice"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value="admin"/>
    </properties>
</persistence-unit>

</persistence>
  • Did you notice that the error `MappingException` is always going to be thrown? (it is the last statement in the method and when the if finishes it will be thrown) – Dazak Mar 03 '17 at 22:52
  • Thanks @Dazak for your comments. And i just noticed that it will always be thrown.. So what does this mean? Its normal, a bug in hibernate library or what? The delete problem get solved by removing "cascade = CascadeType.ALL" at account in Person class. But the exception at debug is still there. – Fawad Khaliq Mar 04 '17 at 07:53
  • @Dazak there are still one return in that method, so why always? – ByeBye Mar 04 '17 at 11:16

0 Answers0