在@Table(name =“ tableName”中-使“ tableName”成为JPA中的变量]

问题描述 投票:15回答:5

我正在使用JPA,我需要将“ tableName”设置为变量。

在数据库中,我有很多表,我的代码需要访问指定要读取的表。

@Entity
@Table(name = "tableName")
public class Database implements Serializable {...............}

有什么想法吗?

java jpa ejb-3.0
5个回答
7
投票

[您可以这样做,如果您担心的话,我想。 从未尝试过,这只是一个疯狂的猜测。但这是通常的做法-我遵循​​“命名查询”;是的,这完全是另一回事。

@Entity
@Table(name = Database.tableName)
public class Database implements Serializable {
    public static final String tableName = "TABLE_1";
    ...............
}

但是我不明白为什么有人会那样做。你能告诉我们你在做什么吗?为什么您有几个表的定义完全相同?

[[已编辑]

我尝试了您的解决方案。它没它说:注释属性Table.name必须是一个常量表达式。

所以,还不够清楚吗?我的意思是你不能那样做。而且我认为这很合逻辑。如果要让Hibernate生成架构,则可以在架构中并使用适当的关系定义所需的所有实体。


3
投票

如果只想引用/读取表名,则可以使用下面的代码。如果要更改,则无法按照Pascal所说。

@Entity
@Table(name = Database.tableName)
public class Database implements Serializable {
    public static final String tableName = "TABLE_1";//this variable you can reference in other portions of your code. Of course you cannot change it.
    ...............
}

2
投票

无法在运行时指定表名,这根本不是JPA的工作方式(而且我仍然不确定要满足您的要求)。要么根据您的表集映射不同的实体,然后运行各种查询,要么根据客户端的输入动态地构建它们(可能使用Criteria API)(可能使用Criteria API)。

如果要从其他表中选择数据,则>>

然后您可以使用:

@Subselect("")

而不是:

@Table(name = "tableName")

我有解决方法。它使用javax.persistence.EntityManager和String.format做到这一点。

package com.example.test.dao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.List; import javax.persistence.EntityManager; @Component public class SomeDao { @Autowired EntityManager em; public List<?> listFoodMoneyDateOfPayment(int departmentId, String sumKey, String tableName) { String s = "SELECT SUM(%s) AS money, CONCAT(YEAR(apply_time), '-', MONTH(apply_time)) AS yearmonth " + "FROM (%s) WHERE department_id = %d GROUP BY yearmonth"; String sql = String.format(s, sumKey, tableName, departmentId); System.out.println(sql); List<?> test = em.createNativeQuery(sql).getResultList(); return test; } }

调用代码是:

@RestController @RequestMapping("/api") public class TestController { @Autowired private SomeDao dao; @RequestMapping("/test2") public HttpEntity test2() { var l = dao.listFoodMoneyDateOfPayment(12, "food_payment", "payment_application"); System.out.println(l.getClass()); System.out.println(JSON.toJSONString(l)); return ResultBean.success(); } }

而且效果很好。但是您应该检查传入的参数。

0
投票
如果要从其他表中选择数据,则>>

然后您可以使用:


0
投票
我有解决方法。它使用javax.persistence.EntityManager和String.format做到这一点。

package com.example.test.dao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.List; import javax.persistence.EntityManager; @Component public class SomeDao { @Autowired EntityManager em; public List<?> listFoodMoneyDateOfPayment(int departmentId, String sumKey, String tableName) { String s = "SELECT SUM(%s) AS money, CONCAT(YEAR(apply_time), '-', MONTH(apply_time)) AS yearmonth " + "FROM (%s) WHERE department_id = %d GROUP BY yearmonth"; String sql = String.format(s, sumKey, tableName, departmentId); System.out.println(sql); List<?> test = em.createNativeQuery(sql).getResultList(); return test; } }

© www.soinside.com 2019 - 2024. All rights reserved.