【问题标题】:@Column name ignored JPA@Column 名称忽略了 JPA
【发布时间】:2016-01-12 16:35:15
【问题描述】:

将 Google App Engine 与 MySQL 结合使用

JPA 忽略了我的列别名

我尝试过声明变量:

@Column(name = "fk_location_id") 
private Long locationId;

我也尝试过在 getter 上声明:

@Column(name = "fk_location_id") 
public Long getLocationId() {
    return locationId;
}

我也尝试过同时声明

没有工作。只是抛出错误:

Caused by: org.datanucleus.store.rdbms.exceptions.MissingColumnException: Required columns missing from table fk_location_id

我用谷歌搜索并找到了与 Hibernate 相关的东西,但不相信我在使用它?

这是我的 persistence.xml 文件

<property name="datanucleus.ConnectionDriverName" value="com.mysql.jdbc.Driver" />
<property name="datanucleus.ConnectionURL" value="jdbc://mydb..." />
<property name="datanucleus.ConnectionUserName" value="user" />
<property name="datanucleus.ConnectionPassword" value="pass" />
<property name="datanucleus.autoStartMechanism" value="None" />
<property name="datanucleus.validateTables" value="true" />
<property name="datanucleus.validateConstraints" value="true" />
<property name="datanucleus.validateColumns" value="true" />
<property name="datanucleus.DetachAllOnCommit" value="true" />
<property name="datanucleus.maxFetchDepth" value="1" />
<property name="datanucleus.storeManagerType" value="rdbms" />

这是表的架构:

CREATE TABLE `sites` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
`fk_location_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

我如何查询数据库:

EntityManagerFactory factory;
factory = Persistence.createEntityManagerFactory(Globals.PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();

Query q = em.createQuery("select t from Site t");
List<Site> siteList = q.getResultList();

Java 类:

@Entity
@Table(name = "sites")
public class Site {

private Long id;
private String name;
private Long locationId;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
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;
}


public Long getLocationId() {
    return locationId;
}

public void setLocationId(Long locationId) {
    this.locationId = locationId;
}

}

错误告诉 FKLOCATIONID 列未找到,因此它应该在搜索 fk_location_id 时搜索 FKLOCATIONID

【问题讨论】:

  • @NeilStockton 好的,我已经通过持久性文件进行了更新。我有 jdoconfig 和持久性,因为那是项目模板附带的?不确定它是否有害。然而,即使从持久性文件中删除冗余代码后,列映射问题仍然存在,有什么线索吗?我也不确定eclipse链接是做什么的,所以我也把它拿出来了。我剩下的仍然是我的数据,只是列别名不起作用
  • 好的,没问题,不知道。此外,您的声望量使您能够编辑其他人的问题并删除标签
  • 然后,当您完成此操作后,发布您正在使用的软件版本,发布您用于生成数据库的代码(DataNucleus SchemaTool?,JPA 2.1 Schema Generation?,生成在飞行中)。然后发布生成的 DDL 以生成架构(在日志中)。
  • 我通过一系列 SQL 语句(创建表等)创建了我的数据库。然后我使用它连接到我已经创建的数据库并将数据映射到对象中。所以不确定我是否使用任何工具?看不到任何日志。我更新了我的问题以显示我如何查询数据库。
  • 如果您通过其他方式生成数据库,那么请告诉我们该实体的架构是什么(如果您通过其他方式生成架构,那么为什么您有持久性属性来生成架构???)。如果您提交查询,请告诉我们为该查询发出的 SQL 是什么

标签: java google-app-engine jpa datanucleus


【解决方案1】:

确保@Column 注释来自javax.persistence 而不是来自JDO 或其他地方。 DataNucleus 只能使用 JDO 或 JPA 注释,但不能同时使用两者

【讨论】:

    猜你喜欢
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多