In @Table(name = “tableName”) - make “tableName” a variable in JPA

后端 未结 5 1377
無奈伤痛
無奈伤痛 2020-12-16 14:35

I am using JPA and I need to make the \"tableName\" a variable.

In a database, I have many tables, and my code needs to access the table where I specify it to read.

相关标签:
5条回答
  • 2020-12-16 15:18

    You can do something like this, if thats your concern, I guess. Never tried it, its just a wild guess. But thats the usual practice -- I follow for Named Queries; yes, that's a different thing altogether.

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

    But I don't see why anyone would do that. Could you tell us what are you up to? Why you have few tables exactly same definition?

    [Edited]

    I tried your solution. It did not work, it says: The value for annotation attribute Table.name must be a constant expression.

    So, isn't that clear enough? I mean you can't do that. And I believe its quite logical. If you want Hibernate to generate your schema then you can define all the entities you want, in the schema, and with the appropriate relationships.

    0 讨论(0)
  • 2020-12-16 15:20

    Specifying the table name at runtime is not possible, this is simply not how JPA works (and I'm still not sure to get your requirement). Either map different entities on your set of tables and run various queries or build them dynamically (maybe using the Criteria API) depending on the input from the client side or use something else than JPA (like iBATIS).

    0 讨论(0)
  • 2020-12-16 15:22

    If you want to select data from different tables,

    then you can use :

    @Subselect("")

    instead of :

    @Table(name = "tableName").

    0 讨论(0)
  • 2020-12-16 15:26

    If you want only to reference/read the table name, it is possible as in the code below. If you want to change, it is not possible as Pascal said.

    @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.
        ...............
    }
    
    0 讨论(0)
  • 2020-12-16 15:27

    I have a workaround.
    It uses javax.persistence.EntityManager and String.format to do that.

    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;
        }
    }
    
    

    The invoke code is that:

    @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();
        }
    }
    
    

    And it works well.
    But you should check the arguments passed in.

    0 讨论(0)
提交回复
热议问题