Question

I have some problems with @Embeddable in JAVA JPA. I have an entity class named "Author":

@Entity

    @Table(name = "author")
    @XmlRootElement
    @NamedQueries({
        @NamedQuery(name = "Author.findAll", query = "SELECT a FROM Author a"),
       ...})


    public class Author implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @Basic(optional = false)
        @Column(name = "aID")
        private Integer aID;
        @Column(name = "aName")
        private String aName;
        @Column(name = "aSurname")
        private String aSurname;
        @Column(name = "aPhone")
        private Integer aPhone;


        @Embedded 
        @AttributeOverrides({

         @AttributeOverride(name="city",column=@Column(name="Address")),
         @AttributeOverride(name="street",column=@Column(table="Address")),
         @AttributeOverride(name="number",column=@Column(table="Address"))
         }) private Address address;

    // set and get methods.
    }

Also I have an Embeddable class named "Address":

@Embeddable
    @Table(name = "Address")
    @XmlRootElement
    public class Address implements Serializable 
    {
       private static final long serialVersionUID=1L;
        @Column(name="city")
        private String city;
        @Column(name="street")
        private String street;
        @Column(name="number")
        private int number;

    // get and set methods.
}

In my main class I want to insert this values to the database. (I use mySQL) But I am getting an error on this line: em.getTransaction.commit();

    public class CreateAuthor extends javax.swing.JFrame {

        private static final String PERSISTENCE_UNIT_NAME = "Project";
        private static EntityManagerFactory emf;

        public void CreateAuthor() {
            initComponents();
        }


        private void ekleButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
            emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
            EntityManager em = emf.createEntityManager();


            em.getTransaction().begin();


            Author author = new Author();
            author.setAID(3);
            author.setAName("Sheldon");
            author.setASurname("Smith");
            author.setAPhone(768987);
            Address adr = new Address();



             adr.setCity("Paris");
             adr.setStreet("cinar");
             adr.setNumber(12);
             author.setAddress(adr);



            em.persist(author);

            em.getTransaction().commit();  /// error occured

            em.close();       

    }
}

On my database side, I have Author table (aID(pk),aName,aSurname,aPhone)

Address Table (city,street,number)

Do you have any idea why an error is occured?

Was it helpful?

Solution

The goal of Embeddable is to have fields of an object (Address) stored in the same table as the entity's table (Author -> author).

If you want to save them in another table, than Address should be an entity on its own, and there should be a OneToOne or ManyToOne association between Author and Address. The mapping, as is, don't make any sense.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top