Hi guys im new to this forum, i want to know how can one update one table using one composite key that is being used by two different enties ok here is the scenario :
@Embeddable public class PrimaryKey implements Serializable, Comparable<PrimaryKey> { @Column(name = "isn_1", updatable = false) private int id1; @Column(name = "isn_2", updatable = false) private long id2; public PrimaryKey() { } public PrimaryKey(int id1, long id2) { this.id1 = id1; this.id2 = id2; }
public class TableToBeUpdated @OneToOne @LazyToOne(LazyToOneOption.NO_PROXY) @Fetch(value = FetchMode.JOIN) @JoinColumns({ @JoinColumn(name = "ISN1", referencedColumnName = "isn_1"), @JoinColumn(name = "ISN2", referencedColumnName = "isn_2") }) private CurrentTable currentTable; //this is the new table we want to use for updating this table for the new projects @OneToOne @LazyToOne(LazyToOneOption.NO_PROXY) @Fetch(value = FetchMode.JOIN) @JoinColumns({ @JoinColumn(name = "ISN1", referencedColumnName = "n_isn1"), @JoinColumn(name = "ISN2", referencedColumnName = "n_isn2") }) private NewTable newTable;
@Entity @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) public class CurrentTable extends implements Serializable, Comparable<CurrentTable> {
@EmbeddedId private PrimaryKey primaryKey = new PrimaryKey();
and this is what we have in the database in terms of the composite primary keys
TableToBeUpdate{ ISN1 and ISN2 } CurrentTable{ isn_1 and isn_2 }
now in our new project we no longer want to update “TableToBeUpdated” with “CurrentTable” but with “NewTable” composite primary key;
NewTable{ @EmbeddedId private PrimaryKey primaryKey = new PrimaryKey(); }
coloums are : NewTable{ n_isn1 and n_isn2 }
but when I test this, I get the following error......
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [_SchedulerTest.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: test] Unable to build EntityManagerFactory at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) at Test.main(Test.java:22) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) Caused by: javax.persistence.PersistenceException: [PersistenceUnit: test] Unable to build EntityManagerFactory at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677) at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:132) at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:225) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:308) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417) ... 17 more Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: TableToBeUpdated column: ISN1 (should be mapped with insert="false" update="false") at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:670) at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:692) at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:714) at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:468) at org.hibernate.mapping.RootClass.validate(RootClass.java:215) at org.hibernate.cfg.Configuration.validate(Configuration.java:1149) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1334) at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867) at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669) ... 22 more
It works well if I do the following:
public class TableToBeUpdated @OneToOne @LazyToOne(LazyToOneOption.NO_PROXY) @Fetch(value = FetchMode.JOIN) @JoinColumns({ @JoinColumn(name = "ISN1", referencedColumnName = "isn_1",updatable = false,insertable = false), @JoinColumn(name = "ISN2", referencedColumnName = "isn_2",updatable = false,insertable = false) }) private CurrentTable currentTable;
@OneToOne @LazyToOne(LazyToOneOption.NO_PROXY) @Fetch(value = FetchMode.JOIN) @JoinColumns({ @JoinColumn(name = "ISN1", referencedColumnName = "n_isn1"), @JoinColumn(name = "ISN2", referencedColumnName = "n_isn2") }) private NewTable newTable;
but now we would like to update the table using both tables. Can anyone help me. I know I might not be making sense but I think thats the best explanation I could give.
|