Create Table Using POJO class spring data cassandr - spring

Create Table from Casssandra POjo class using spring data Cassandra is there any tag to create a table definition

Simply use annotation #Table from org.springframework.data.cassandra.core.mapping.Table and also use #PrimaryKey(value = "id") if your pojo have attribute of id.
For an CassandraConfiguration.class, follow my answer here.

Related

Is it necessary to use Entity annotation for Select query records from Database

I have spring boot application with JPA and MySQL. The table is already created and data are present. From the spring boot application, we need to create two API - get a particular record using id and get all the records. I created a model class but confused with usage of #Entity. Because I'm not going to insert/delete a record from the database. I want to use only for select the record.
For Select query (findBy), do we need to use #Entity annotation at the top of model class?
Yes, you would need to use #Entity to mark your model class, this tells JPA that your class is used for mapping to and from a database table. If you want to make sure that you don't accidentally overwrite the data, you could implement a check via #EntityListener, as described in this answer.

Find entity class by using its table name

I have the name of a table .
I want to retrieve entity class of this table in Spring JPA.
The table name may differ from its entity class name.
I searched for a lot, but the solutions were to find the table name using the entity class . That is, quite the inverse is what I need.
Do you have any suggestions for this problem?
Assuming you know how to map entity class to a table name, you can get a list of all entity classes using JPA metamodel API, and subsequently build a complete inverse mapping.

jpa Hibernate make schema name configurable in Entity class

I was developing a simple Spring Boot application in which, jpa + Hibernate is user for accessing my data source, which is Oracle DB. The entity class is given below.
#Entity
#Table(name="MY_SCHEMA.MY_DB")
public class Member implements Serializable {
.............
}
Currently my project doesn't have any persistence.xml. The problem is, I need to make the schema name (MY_SCHEMA) inside #Table annotation configurable, that is getting the schema value from application.properties file on run time.
I have tried by adding spring.jpa.properties.hibernate.default_schema=schema option in application.properties file. But all in vain.
Update
Have added more details in another question Hibernate how to make schema name configurable for entity class
Below are the available options that can be used for your purpose of creating table in a particular Schema. mention the schema name in the schema field.
#Entity
#Table(name = "MY_TABLE_NAME", schema= "MY_SCHEMA_NAME")
public class Myclass {
Also you can define the schema name in the DB URL as below using the application.properties file. you need to update the values as per your needs.
spring.datasource.url=jdbc:mysql://localhost:3306/MY_SCHEMA_NAME?autoReconnect=true&useSSL=false

How to change which field is used as _id without annotations in Spring data MongoDB

I have some classes which are being generated from a WSDL using the CXF wsdl2java tool.
I would like to store instances of these classes in a MongoDB database, using Spring data MongoDB.
The default mapping is acceptable for this, except for one thing:
I would like to change which field is used as _id.
Normally this is done with a annotation like #Id.
But because these classes are generated, I would like to do this without an annotation.
Is there a (correct?) way to do this?
So my generated class is:
class Simple {
String businessId;
String otherfield1;
.
String otherfield999;
}
And I would like Spring data MongoDB to use 'businessId' as the '_id' field in MongoDB, without changing the 'Simple' class by adding an annotation.
Thanks!
That's currently not supported. The property either needs to be id, _id or annotated with #Id.

How can I get the fieldnames of the PK (#Id) of a JPA #Entity

I'm using JPA2 (Hibernate 4) and have a bunch of #Entity classes with my PKs defined using #Id. How can I programatically retrieve the fieldnames of the PKs of each entity class?
The business case for this is that I want to use Spring's BeanUtils.copyProperties method to copy over entity objects, but want to make sure that I ignore the id fields of my entities while copying.
Is there a way to find this information? Does JPA provide this information? I realize that I can use reflection to go through every class, but hoped there would be an easier solution. I looked into scanning the PropertyDescriptors of the class, but cannot seem to find any method on the PropertyDescriptor that would give me the annotations of a property.
Try this:
#PersistenceUnit
private EntityManagerFactory emf;
Metamodel metamodel = emf.getMetamodel();
metamodel.entity(MyEntity.class).getId();

Resources