I am a newbie to Apache Flink. I am using Pojo Sink to load the data into Cassandra. Right now, I am specifying table and keyspace names with the help of #Table annotation.
Now, I want to pass table name and keyspace name dynamically on run time so that I can load data into tables specified by user. Is there any way to achieve this?
#Table is a CQL annotation that defines which table this class entity maps to. AFAIK, currently there is no way to make it dynamically mapped to any table at run time, because it would infer to the class name if the name is not specified, i.e.
#Table
public class MyTable {...}
infers a table 'mytable'
#Table(table = "another_table")
public class AnotherTable
infers the table 'another_table'
Related
I have one table which has all the api audit information - Table name : api_audit
I have one table which has extra information about every api call - Table name : api_audit_info
Inside api_audit I have primary key as "transaction_id".
I want all the data from api_audit table and some data from api_audit_info table.
I have written a custom query like -
#Query(select c from ApiAudit c INNER JOIN ApiAudiInfo t ON c.transactionId = t.msgId)
But the issue is that the result type that I am getting this way contains only ApiAudit type data.
What shall I do to get data from both the tables. Please help.
Note: I am using JpaRepository as I need paginated data.
I am fairly new to Spring boot and JPA so not sure exactly which direction to look to.
Whenever I need to join data from more than 1 table, I am using Jdbi.
Here you have the official documentation:
Remember to include the required dependencies and configure a bean for Jdbi in your project.
Then I create a repository class, POJO and query with all of the information which I need. For example:
select c.transaction_id as transactionId, t.name as name from ApiAudit c INNER JOIN ApiAudiInfo t ON c.transactionId = t.msgId
Here you have some code samples from official documentation
After you map your data to POJO, you can use
public PageImpl(List<T> content, Pageable pageable, long total)
to return paginated data.
There is a big chance that there is a better solution, but this works for me every time.
I'm doing an exercise with Springboot that basically consists of a simple application with a Rest Controller who has to store an object received from POST request to a mysql db using JPA/Hibernate.
My problem is the following one:
the table has this structure:
And I have this pojo which has to map the table as an object:
If you pay attention, the table has a column named "CARD_HOLDER_FULL_NAME", but in the app, the card holder must be a separate object:
so, how do I specify that the fullName attribuite in the CaldHolderInfo class represents that column in the table?
I'm very rusty with Springboot/JPA/hibernate so I don't know how to proceed
You need to make CaldHolderInfo an Embeddable.
See JPA #Embedded And #Embeddable
And please, no images of code on this site.
I'm trying to get the postgresql table column name. I've tried using information_schema but it doesn't work on my java mvc project. What should I do ?
This is actually my first question on StackOverflow, so I'm sorry if my question is difficult to understand. Thank you!
public interface MyFileRepository extends JpaRepository<MyEntityModel, Long> {
#Query("select column_name,data_type from information_schema.columns where table_name = 'MyEntityModel';")
List<MyEntityModel> myList();
}
About schema. You can set default schema by two ways:
Put it to the properties(or .yml file) spring.datasource.schema = #your schema name
Put it in your entity model #Table(schema = "SCHEMA_NAME")
About getting information about table you can look this one open question:
Is there any Simplest way to get Table metadata (column name list) information, in Spring Data JPA ? which could I use on universal database
Our project is using MyBatis for persisting Java Class Objects to tables and I have a table CUSTOMER_VACATION_PLAN but I need to map this table name to CustomerVacationPlan or simply Vacation. In JPA I used #Table(name="") to do that.
How would I do that in MyBatis?
In mybatis there is absolutely no coupling between table name and class name. The mapping is specified between result set (regardless of which table(s) or view(s) it is constructed from) and class.
<select id="selectVacations" resultType="Vacation">
SELECT * FROM CUSTOMER_VACATION_PLAN WHERE ID = #{id}
</select>
You need to read mybatis mapping documentation as it is very explicit about that.
I have some tables in db(postgresql) with names like this "Test".When i try create java classes from this tables with hibernate its not happening. I get classes from tables with names like this test. How to make hibernate can see tables with quotes in names?
UPDATE
Maybe i write question not correct. But i cant create java classes and i want to know how to do reverse ingenering with tables which have names in qoutes. I cant delete qoutes from table names and column names couse they have names like Type and Full.
By default, Hibernate assumes the database table name is the same as the class name, but you can override this behaviour via the #Table annotation:
#Entity
#Table(name="\"Test\"") // Will use "Test" (including the quotes) as the table name
public class Test {
The #Table annotation is used to specify the table to persist the data. The name attribute refers to the table name. If #Table annotation is not specified then Hibernate will by default use the class name as the table name. So your database's table name is "Test" then you should use your class name is "Test".
Please check your database with
select * from """Test""" if your table name is "Test".
Your entity class should be
#Entity
#Table(name = "\"\"\"Test\"\"\"")
public class Test {
}
It seems that you are not using annotations as of now...
So in case you want to use "Test" as table name you should define the mapping of POJO with your table either via annotations as defined by Bohemian
#Entity
#Table(name="\"Test\"")
public class Test {
or define in the Test.hbm.xml in which you have to map your table and fields to java class and columns.
Alternately, you can specify the schema and database name inside #Table annotation.
#Entity
#Table(name = "Test", schema = "public", catalog = "TestDatabase")
Hibernate will recognize the table without the need to escape double quotes.