How to dynamically add columns in Database? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
I have a spring boot project with Mysql & Hibernate, all I need is to provide a privilege to the user to change or alter columns of the database dynamically(runtime), I haven't found any good approach for that XML approach is quite hectic and will require a lot changes to be made.
Example:
A table User with columns
"Username" & "Password"
And from UI client will add another column "email" so it should make changes in Database table.

What you are specifically asking here would need changes to your entities during runtime, and that is not possible. You can achieve a similar solution by adding one additional column customerAttributes to the User table and store that information as map:
#Convert(converter = HashMapConverter.class)
private Map<String, Object> customerAttributes;
There is a good tutorial for that: https://www.baeldung.com/hibernate-persist-json-object

Related

Can any one please suggest me the best way of retrieving the records in hibernate [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am working on a batch application where I have to retreive millions of records and need toprocess it individually. We are using hibernate to connect to DB.
I have tried using HQL and Criteria API to fetch the records(using the query Select * from table_name).
But it is taking more than 5 min .Sometimes it is getting hanged.
Can anyone suggest me the best approach for the data retrieving.
You may want to employ cursors, and if your queries are as simple as you indicated, pure JDBC may be sufficient - like here: https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html
Also, consider adding the WHERE clause to eliminate rows that can be recognized as irrelevant for your purposes.
And depending on your table and queries, adding an index may also help.
If you need to use hibernate, you can use ScrollableResults as mentioned here: https://stackoverflow.com/a/9891008/455449

Java model best practices SQL - JSON -VIEW [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
While working on a Java web application I was wondering if my model layer was written as it should be. For instance, let's say we have a table USER in our SQL database which consists of 15 columns. Now, when we SELECT all of the columns with SQL we map it to a Java class, serialize via JSON and send it via network to some View and show it on screen.
In a second scenario, we want to select only 2 columns on screen so we do SELECT c1,c2 FROM USER. Thats where my question comes in... am I supposed to map those columns to a same Java model class? Or should i create a new mapper and class to fit it? Both of the approaches seem to have drawbacks, separate class for each query is more work, but it makes sure you always know what data it contains, rather than checking for nulls or working with optionals, also it prevents you from mapping columns you actually don't need.
What is your opinion? Thanks a lot!
Technically you could reuse the same User class for full 15-attribute as well as partial 2-attribute entity. But that will come with a price. Every time you'll see an instance of User class in the code your will have to think if it's the full entity or the partial? Which fields may or may not be null? This will make it much harder to reason about code.

Prevent duplicate entries in column using hibernate [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am trying to make a login system for my app using web-service,hibernate and MySql.What I am trying to do is to make separate column for phone number and email id provided by the user.I want my system to work with both the options available,I mean my login system should work for email id and as well as phone no.If my user wants to login with phone number or email id,in both way it should work.But I don't know how to make both of them unique to prevent duplicate entries.If I am using primary key it is working only with one column.I want both of them to be unique for each user registration.
If I understood what you are asking I think you can annotate the both attributes with:
#Column(unique = true)
It will make your attributes unique, so you will ensure that the data won't be duplicated.

How to show all the items in a MySQL database to a panel without hard coding [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want it to be like this. Inside the panel some details from database. Maybe a loop to do this?
You would generally display data from database tables in a JTable. You need to do an SQL query to get the data from the database and then you copy the data from the ResultSet to a TableModel.
Take a look at the TableFromDatabase.java class found in Table From Database for a generic example of this might be done.
If you are using Netbeans 7.x and above, then Right Click on the desired package (or better create one) -> New -> Other -> Swing GUI forms -> MAster Detail Sample Form.
Proceed forward, establish a database connection through the wizard, choose which entities you wish to display and let Netbeans do the rest for you.

friend request and friendship table, I have an idea is it OK? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have my own server, that get from the client friend requests:
- case 1:
user001 sends friend request to user002
1. I create table called user001 in my server, with two colunms: friendID,status(accepted,blocked ect..)
and insert "user002" as a value of friendId column.
2.and create another table named user002 and insert user001 as a value of friendId column.
so every user has his own table with list of his friends, is this good way, or there is another way...
I choose this way, to make it easy to retrive friend list and another info. about a spicific friend.
You should only create one table, with columns UserId, friendId, Status. That way you can store all data in one table. To get user1's friends, filter on user1's UserId.

Categories

Resources