我的问题是当我保存一个人住的人记录的身份是1和地址2时,当我保存另一个记录时,人的身份证是3而且住所4等增加了,我想增加它应该是记录谁是1,注册代理1注册是2和2下一个3和3,依此类推。
然后我留下实体的源代码,main,要保留的方法以及在创建表时是什么给我控制台。我正在使用netbeans 7.4和EclipseLink库jpa2.1和jdbc用于sqlite dirver
@Entity
public class Persona implements Serializable {
protected static final long serialVersionUID = 1L;
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long id;
private String nombre;
@OneToOne (cascade = CascadeType.ALL)
private Domicilio domicilio;
public Persona() {
}
public Persona(String nombre) {
this.nombre = nombre;
domicilio= new Domicilio();
}
public Domicilio getDomicilio() {
return domicilio;
}
public void setDomicilio(Domicilio domicilio) {
this.domicilio = domicilio;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
实体Domicilio:
@Entity
public class Domicilio implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String calle;
public Domicilio() {
}
public Domicilio(String calle) {
this.calle = calle;
}
public String getCalle() {
return calle;
}
public void setCalle(String calle) {
this.calle = calle;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
PersonaJpaController类中用于保存记录的方法
public void guardar(){
persona= new Persona("angel");
persona.getDomicilio().setCalle("alpatacal");
create(persona);
}
这是我的主要计划
public class Pruebaentidades {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("pruebaentidadesPU");
EntityManager em = emf.createEntityManager();// create tables
PersonaJpaController p= new PersonaJpaController(Persistence.createEntityManagerFactory("pruebaentidadesPU"));
p.guardar();
}
}
这就是我创建表时的控制台
[EL Info]: 2014-01-03 17:27:01.189--ServerSession(24979675)--EclipseLink, version: Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5 [EL Info]: connection: 2014-01-03 17:27:01.379--Not able to detect platform for vendor name [SQLite3]. Defaulting to [org.eclipse.persistence.platform.database.DatabasePlatform]. The database dialect used may not match with the database you are using. Please explicitly provide a platform using property eclipselink.platform.class.name. [EL Info]: connection: 2014-01-03 17:27:01.446--ServerSession(24979675)--file:/C:/Users/VM/Desktop/Mis Proyectos/Proyectos Netbeans/pruebaentidades/build/classes/_pruebaentidadesPU login successful [EL Warning]: 2014-01-03 17:27:01.6--ServerSession(24979675)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLException: near "CONSTRAINT": syntax error Error Code: 0 Call: ALTER TABLE PERSONA ADD CONSTRAINT FK_PERSONA_DOMICILIO_ID FOREIGN KEY (DOMICILIO_ID) REFERENCES DOMICILIO (ID) Query: DataModifyQuery(sql="ALTER TABLE PERSONA ADD CONSTRAINT FK_PERSONA_DOMICILIO_ID FOREIGN KEY (DOMICILIO_ID) REFERENCES DOMICILIO (ID)")
答案 0 :(得分:0)
对于那些有同样问题的人,我想说问题的解决方案是为每个表创建一个序列并使用策略生成的安全性。 为每个实体添加这些语句,允许我为每个表创建一个序列
实体角色:
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator =“PERS_SEQ”) @SequenceGenerator(name =“PERS_SEQ”,sequenceName =“PERS_SEQ”)
实体Domicilio:
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator =“DOM_SEQ”) @SequenceGenerator(name =“DOM_SEQ”,sequenceName =“DOM_SEQ”)