Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
958 views
in Technique[技术] by (71.8m points)

mysql - How to cascade persist using JPA/EclipseLink

I am having problems performing a cascade persist operation on a parent entity. When the child entity is persisted, the reference (generated id) to the parent entity is null. How would I get this to persist correctly?

Entities:

@Entity
public class Contact {

        @Id @GeneratedValue(strategy=GenerationType.TABLE, generator="contact_gen")
        @TableGenerator(name="contact_gen",
            table="id_gen", pkColumnName="gen_name", 
            valueColumnName="gen_val", pkColumnValue="cont_gen")
        @Column(name="contact_id")
        private Long id;

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

        @OneToMany(mappedBy="contact", cascade=CascadeType.PERSIST)
        private List<Address> addresses = new ArrayList<Address>();

        public void addAddress(Address address) {
             addresses.add(address);
        }

        ...
}

@Entity
public class Address {

        @Id @GeneratedValue(strategy=GenerationType.TABLE, generator="address_gen")
        @TableGenerator(name="address_gen",
            table="id_gen", pkColumnName="gen_name", 
            valueColumnName="gen_val", pkColumnValue="addr_gen")
        @Column(name="address_id")
        private Long id;

        @Column(name="full_address")
        private String fullAddress;

        @ManyToOne
        @JoinColumn(name="contact_id")
        private Contact contact;

        ...
}

Service:

@Stateless
public class ContactService {

    @PersistenceContext
    private EntityManager em;

    public void createContact() {   
        Contact contact = new Contact();
        contact.setName("Michael Scott");
        contact.addAddress(new Address("1725 Slough Avenue");
        em.persist(contact);        
    }

}

MySQL Tables & Inserts:

CREATE TABLE `contact` (
  `contact_id` int(11) NOT NULL,
  `name` varchar(45) NOT NULL
  PRIMARY KEY (`contact_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `address` (
  `address_id` int(11) NOT NULL,
  `full_address` varchar(100) NOT NULL,
  `contact_id` int(11) NOT NULL,
  PRIMARY KEY (`address_id`),
  KEY `FK_ADDRESS_contact_id` (`contact_id`),
  CONSTRAINT `FK_ADDRESS_contact_id` FOREIGN KEY (`contact_id`) REFERENCES `contact` (`contact_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE id_gen (
    gen_name VARCHAR(80),
    gen_val INT,
    PRIMARY KEY (gen_name)
);
INSERT INTO id_gen (gen_name, gen_val) VALUES ('cont_gen', 0);
INSERT INTO id_gen (gen_name, gen_val) VALUES ('addr_gen', 0);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sadly, you're not showing the content of addAddress. Since your association is bidirectional, are you setting "both sides of the link" in this method? Something like this:

@Entity
public class Contact {

    ...

    @OneToMany(mappedBy="contact", cascade=CascadeType.PERSIST)
    private List<Address> addresses = new ArrayList<Address>();

    public void addToAddresses(Address address) {
        address.setContact(this);
        this.addresses.add(address);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...