0

I am currently building a e-commerce website using spring mvc4 hibernate4 and Mysql where the Admin should be able to dynamically add a column to a particular table when the application is already running and the server should not be restarted.For example if a new value is inserted in table 1 then a column should be created for it in table 2.

  1. Is there any way to configure hibernate 4 with JPA to dynamically add a column at runtime?
  2. And if yes can you give me a sample code?
  3. And if no what are the alternate ways of achieving my goal of dynamically adding columns?
5
  • kiview is right, there is likeley no JPA/Hibernate solution. I think you could handle your task with Spring JDBC Teample and a custom RowMapper docs.spring.io/spring/docs/current/spring-framework-reference/… Commented Feb 19, 2016 at 10:22
  • What are you trying do here? In 15 years as a developer I have never seen anyone with a requirement to update a database SCHEMA is response to a data update. If you want to work with a dynamic schema maybe a document database is a better option than an SQL database. Commented Feb 19, 2016 at 13:23
  • Consider a scenario where a vendor adds a product to a database with certain deal variation parameters .As a product can be anything such as a bike,car,Iphone with different variations i cannot have all the possible variations stored in the database as i will not know all the variations of all products only during runtime it can be determined by the vendor who adds the product so if i have a table which contains product variations i wanted to add a new column if a particular variation is not present in the existing schema to achieve this i need to dynamically add a column Commented Feb 19, 2016 at 17:20
  • What you are talking about is something like a 'variation' column? Just use a Many-To-One relation from your product-table to your variation-table (which can be mapped by the ORM as well). Commented Feb 19, 2016 at 21:51
  • But the problem is that each variation of product might effect the price so if i use one to many relation from product to variation it will be difficult to get a particular variation selected by the user grouped by a common price as there is a possibility that each variation might have a different price. Commented Feb 20, 2016 at 6:08

2 Answers 2

1

I think there can be no JPA/Hibernate solution for this, since your Entities map to your database schema and what you are trying to achieve is simultanously mutating your Entity's Class and the corresponding schema. You need to go with JDBC and plain SQL for this and use DDL statements.

I wonder what's your use case for this?

Sign up to request clarification or add additional context in comments.

2 Comments

so in such a case what option do i have if i want to have variable number of columns in a particular table ?is there any way ? or is it that any change in column should be done through entity class hard coded.
You simply can not map this behaviour with an ORM (AFAIK), so as I said before, just use SQL with DDL statemts.
0

Consider this scenario If this might helps:

Here is the link of this scenario.

A table User with columns "Username" & "Password"

And from UI client will add another column "email" so it should make changes in Database table.

You can Try:

 @Entity
 @Table(name = "users")
 public class User {
    @Id
    private String username;
    private String password;
    
    @OneToMany(targetEntity = UserDetails.class,
        cascade = CascadeType.ALL,
        orphanRemoval = true,
        fetch = FetchType.EAGER)
    @JoinColumn(name = "username",
        referencedColumnName = "username")
    private List<UserDetails> userDetails;
    .....
}

where your User details class would be like

@Entity
@Table(name = "user_details")
public class UserDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    private Long id;
    
    private String userDetailsType;
    private String userDetailsValue;
    
    .....
}

and the output value will be like

{
  "username": "test",
  "password": "password123",
  "userDetails": [
    {
      "userDetailsType": "email",
      "userDetailsValue": "[email protected]"
    },
    {
      "userDetailsType": "address",
      "userDetailsValue": "some value..."
    }
  ]
}

This will help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.