Java 在此ResultSet-JPA中找不到列名X
所以我有一个公司实体,每次我添加一个公司时都会出错。 我在数据库中使用PostgreSQL,并使用双引号文本在pascalcase中映射表的列 公司实体:Java 在此ResultSet-JPA中找不到列名X,java,postgresql,jpa,wildfly,Java,Postgresql,Jpa,Wildfly,所以我有一个公司实体,每次我添加一个公司时都会出错。 我在数据库中使用PostgreSQL,并使用双引号文本在pascalcase中映射表的列 公司实体: @Entity @XmlRootElement public class Company { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="\"Id\"") private Long id; @Column(n
@Entity
@XmlRootElement
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="\"Id\"")
private Long id;
@Column(name="\"Name\"")
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
公司端点:
@Stateless
@Path("/companies")
public class CompanyEndpoint {
@PersistenceContext(unitName = "primary")
private EntityManager em;
@POST
@Consumes("application/json")
public Response create(Company entity) {
em.persist(entity);
return Response.created(
UriBuilder.fromResource(CompanyEndpoint.class)
.path(String.valueOf(entity.getId())).build()).build();
}
@DELETE
@Path("/{id:[0-9][0-9]*}")
public Response deleteById(@PathParam("id") Long id) {
Company entity = em.find(Company.class, id);
if (entity == null) {
return Response.status(Status.NOT_FOUND).build();
}
em.remove(entity);
return Response.noContent().build();
}
@GET
@Path("/{id:[0-9][0-9]*}")
@Produces("application/json")
public Response findById(@PathParam("id") Long id) {
TypedQuery<Company> findByIdQuery = em
.createQuery(
"SELECT DISTINCT c FROM Company c WHERE c.id = :entityId ORDER BY c.id",
Company.class);
findByIdQuery.setParameter("entityId", id);
Company entity;
try {
entity = findByIdQuery.getSingleResult();
} catch (NoResultException nre) {
entity = null;
}
if (entity == null) {
return Response.status(Status.NOT_FOUND).build();
}
return Response.ok(entity).build();
}
@GET
@Produces("application/json")
public List<Company> listAll(@QueryParam("start") Integer startPosition,
@QueryParam("max") Integer maxResult) {
TypedQuery<Company> findAllQuery = em
.createQuery("SELECT DISTINCT c FROM Company c ORDER BY c.id",
Company.class);
if (startPosition != null) {
findAllQuery.setFirstResult(startPosition);
}
if (maxResult != null) {
findAllQuery.setMaxResults(maxResult);
}
final List<Company> results = findAllQuery.getResultList();
return results;
}
@PUT
@Path("/{id:[0-9][0-9]*}")
@Consumes("application/json")
public Response update(@PathParam("id") Long id, Company entity) {
if (entity == null) {
return Response.status(Status.BAD_REQUEST).build();
}
if (id == null) {
return Response.status(Status.BAD_REQUEST).build();
}
if (!id.equals(entity.getId())) {
return Response.status(Status.CONFLICT).entity(entity).build();
}
if (em.find(Company.class, id) == null) {
return Response.status(Status.NOT_FOUND).build();
}
try {
entity = em.merge(entity);
} catch (OptimisticLockException e) {
return Response.status(Response.Status.CONFLICT)
.entity(e.getEntity()).build();
}
return Response.noContent().build();
}
}
我想您的程序会查找名为“Id”的列,而不是“Id”。您应该这样更改注释
@Column(name="Id")
private Long id;
我会说西班牙语,为了回答这个问题,我使用谷歌翻译:它用java标记了相同的错误,原因是通过rs发送了一个不在select查询中的值,例如(
“按c.id从公司c订单中选择不同的c”,Company.class)代码>可能列中的id拼写错误,并且由于在希望将该数据传递给另一个元素时找不到该id,因此它会标记错误
这是我的代码:
String sql = "select p.nombre_paciente, p.apellido_paciente, p.nacimiento_paciente, p.edad_paciente, p.cedula_paciente, p.profesion_paciente,p.dlaboral_paciente,p.dparticular_paciente,p.tlaboral_paciente,p.tparticular_paciente,c.idcivil,c.tipo,**p.id_paciente** from paciente p inner join civil c on p.idcivil = c.idcivil where upper(nombre_paciente) like '%" + nombre.toUpperCase() + "%'"
+ " order by id_paciente offset " + offset + " limit " + utiles.Utiles.REGISTROS_PAGINA;
System.out.println("--->" + sql);
try (PreparedStatement ps = Conexion.getConn().prepareStatement(sql)) {
ResultSet rs = ps.executeQuery();
String tabla = "";
while (rs.next()) {
tabla += "<tr>"
+ "<td>" + rs.getInt("id_paciente") + "</td>"
+ "<td>" + rs.getString("nombre_paciente") + "</td>"
+ "<td>" + rs.getString("apellido_paciente") + "</td>"
+ "<td>" + rs.getDate("nacimiento_paciente") + "</td>"
+ "<td>" + rs.getInt("edad_paciente") + "</td>"
+ "<td>" + rs.getString("cedula_paciente") + "</td>"
+ "<td>" + rs.getString("profesion_paciente") + "</td>"
+ "<td>" + rs.getString("dlaboral_paciente") + "</td>"
+ "<td>" + rs.getString("dparticular_paciente") + "</td>"
+ "<td>" + rs.getString("tlaboral_paciente") + "</td>"
+ "<td>" + rs.getString("tparticular_paciente") + "</td>"
+ "<td>" + rs.getInt("idcivil") + "</td>"
+ "<td>" + rs.getString("tipo") + "</td>"
+ "</tr>";
String sql=“选择p.nombre_paciente、p.apellido_paciente、p.nacimiento_paciente、p.edad_paciente、p.cedula_paciente、p.profession_paciente、p.dlaboral_paciente、p.dpspecific_paciente、p.tlaboral_paciente、p.tparticial_paciente、c.idcivil、c.tipo、**p.id_paciente**从Pacient p.IDC加入civil=c(nombre_paciente)类似“%”+nombre.toUpperCase()+“%”
+“订单按id_paciente offset”+offset+“limit”+utiles.utiles.REGISTROS_PAGINA;
System.out.println(“-->”+sql);
try(PreparedStatement ps=Conexion.getConn().prepareStatement(sql)){
结果集rs=ps.executeQuery();
字符串tabla=“”;
while(rs.next()){
表A+=“”
+“+rs.getInt(“id\u paciente”)+”
+“”+rs.getString(“nombre_paciente”)+“”
+“”+rs.getString(“apellido_paciente”)+“”
+“”+rs.getDate(“nacimiento_paciente”)+“”
+“+rs.getInt(“edad_paciente”)+”
+“”+rs.getString(“cedula_paciente”)+“”
+“”+rs.getString(“专业人士”)+“”
+“”+rs.getString(“dlaboral\u paciente”)+“”
+“”+rs.getString(“dpu paciente”)+“”
+“”+rs.getString(“tlaboral_paciente”)+“”
+“”+rs.getString(“tparticular\u paciente”)+“”
+“”+rs.getInt(“idcivil”)+“”
+“”+rs.getString(“tipo”)+“”
+ "";
粗体标记的内容不存在,当实现它时,错误消失了。
注:我知道有很多文本谢谢,但这不是我想要的答案,它以小写形式生成列,而不是pascalcase CREATE TABLE test.company(id bigserial非空,名称字符变化(255),约束公司_pkey主键(id))我从未使用过postgres,也没有遇到过大写或小写sql语句的问题。也许此链接帮助可以帮助您:jaysee-谢谢我在pascalcase中实现了命名策略,但仍然有一个错误“此结果集中未找到列名“Id”在db中插入时。@netto您找到解决方案了吗?我也找到了相同的解决方案error@el_pup_le是的,我找到了避免这些错误的技巧,但我不再使用entity manager的insert(忘记所谓的)方法…我只是使用.executeUpdate()方法执行普通sql…您可以得出结论,PostgreSQL默认为“小写”列名称,除非您引用它们。一些JPA提供商提供了一个很好的简单设置来为您自动引用它们。只是不要在列名周围加引号。
String sql = "select p.nombre_paciente, p.apellido_paciente, p.nacimiento_paciente, p.edad_paciente, p.cedula_paciente, p.profesion_paciente,p.dlaboral_paciente,p.dparticular_paciente,p.tlaboral_paciente,p.tparticular_paciente,c.idcivil,c.tipo,**p.id_paciente** from paciente p inner join civil c on p.idcivil = c.idcivil where upper(nombre_paciente) like '%" + nombre.toUpperCase() + "%'"
+ " order by id_paciente offset " + offset + " limit " + utiles.Utiles.REGISTROS_PAGINA;
System.out.println("--->" + sql);
try (PreparedStatement ps = Conexion.getConn().prepareStatement(sql)) {
ResultSet rs = ps.executeQuery();
String tabla = "";
while (rs.next()) {
tabla += "<tr>"
+ "<td>" + rs.getInt("id_paciente") + "</td>"
+ "<td>" + rs.getString("nombre_paciente") + "</td>"
+ "<td>" + rs.getString("apellido_paciente") + "</td>"
+ "<td>" + rs.getDate("nacimiento_paciente") + "</td>"
+ "<td>" + rs.getInt("edad_paciente") + "</td>"
+ "<td>" + rs.getString("cedula_paciente") + "</td>"
+ "<td>" + rs.getString("profesion_paciente") + "</td>"
+ "<td>" + rs.getString("dlaboral_paciente") + "</td>"
+ "<td>" + rs.getString("dparticular_paciente") + "</td>"
+ "<td>" + rs.getString("tlaboral_paciente") + "</td>"
+ "<td>" + rs.getString("tparticular_paciente") + "</td>"
+ "<td>" + rs.getInt("idcivil") + "</td>"
+ "<td>" + rs.getString("tipo") + "</td>"
+ "</tr>";