0

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?

1 Answer 1

1

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.

8
  • what should I do to fix it? To create new fields for address information in author table ?
    – Aysegul
    Aug 2, 2013 at 7:11
  • You should first decide what you want. Do you want the address to be stored in the same table, or in a different one? Both are possible. It's your choice.
    – JB Nizet
    Aug 2, 2013 at 7:13
  • I want to store them in the same table.
    – Aysegul
    Aug 2, 2013 at 7:29
  • When I drop the following: @AttributeOverrides({ @AttributeOverride(name="city",column=@Column(name="Address")), @AttributeOverride(name="street",column=@Column(table="Address")), @AttributeOverride(name="number",column=@Column(table="Address")) }) and add columns to Author table, it starts to run. But why it is not working with override ?
    – Aysegul
    Aug 2, 2013 at 7:39
  • You should first explain what is your desire outcomes with these overrides. What do you want to achieve?
    – JB Nizet
    Aug 2, 2013 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.