I am using Spring and it's automatically creating columns. I want some of columns be created but some not.Here is example where Spring creating all 3 columns but I want only money to be created which is annotated with #Cloumn:
#Column(name = FLD_MONEY,unique = false,nullable = true)
private FastMoney money;
private String currencyUnit;
private BigDecimal moneyn;
How I can do that?
Another question is that how I can tell to store String instead of Binary Data for this column:
#Column(name = FLD_MONEY,unique = false,nullable = true)
private FastMoney money;
In database it's writing binary data but I want to see String, is it possible or not?
For first question answer is just add annotation #Transient and it won't store in database.Answer gave in comments below the question
Related
I found this question with different programming languages or different database
Im trying to make a portal to generate multiple choice quizes
the front end will send me a huge json at some point there will be an array of Questions objects
evry object should have:
the question
the right answer
a list of wrong answers ( min 2 max 4 )
the id of the CourseSpecifics object...
I need to store (if possible) the list of wrong answers ( Strings ) in a single record of my postgresql db
this is my class
#Entity
#Table(name = "questions")
public class Questions {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id", nullable = false)
private Integer id;
#Column(name ="question",nullable = false)
String question;
#Column(name ="answer",nullable = false)
String answer;
#Column(name ="wrong_answers",nullable = false)
List<String> wrongAnswers;
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name ="fk_specifics",nullable = false)
CourseSpecifics fkSpecifics;
}
This is the exact point where i have some doubts (lets skip the rest for the moment, i know i can improve it )
#Column(name ="wrong_answers",nullable = false)
List<String> wrongAnswers;
im also using spring.jpa.hibernate.ddl-auto=create to generate db from spring structure but its not mandatory
My questions are :
What is the best data type to use on spring in this situation for wrongAnswers ?
What is the best column definition to use on db according to the data type of wrongAnswers ?
You're likely looking for the #ElementCollection annotation. This will allow you to have a list of strings as you have it right now.
However, it use a new table! Which you don't seem to want. Without binding yourself to a single database (by using arrays or something) your best bet may be to write a converter to serialize a list of strings into a single string.
I'd personally revaluate if your single record is a super strong requirement.
I have problem with Hibernate Envers.
I have classes like:
#Entity
#Table(name = "REVINFO")
#RevisionEntity(MyRevisionEntityListener.class)
public class RevEntity {
#Id
#RevisionNumber
#Column(name = "REV", nullable = false, updatable = false)
private Integer id;
#RevisionTimestamp
#Column(name = "REVTSTMP", nullable = false, updatable = false)
private Date timestamp;
#Column(name = "MODIFIED_BY", length = 100)
private String modifiedBy;
#Column(name = "COMMENT", length = 100)
private String comment;
public class MyRevisionEntityListener implements RevisionListener {
#Override
public void newRevision(Object revisionEntity) {
RevEntity a = (RevEntity) revisionEntity;
a.setComment("Some value");
}
}
How can i select every change for entity ID and their "REVINFO" object?
I've got something like this:
List resultList = AuditReaderFactory.get(entityManager)
.createQuery()
.forRevisionsOfEntityWithChanges(ClientType.class, true)
.add(AuditEntity.id().eq(entityId))
.getResultList();
And it's almost work good. I received every "change" but REVINFO looks strange. All fields are null - and there are 1 more object $$_hibernate_interceptor which actually hold "information" but i cannot acces it via code (or i dont know how). See example at the image.
So my question is:
1 - How can i get REVINFO values ?
2 - Do i realy have to use entityManager, or can it be achived with different approach ?
Edit 2:
Correct me if i am wrong, but does forRevisionsOfEntityWithChanges works as Lazy Initialization? I mean, if i try to receive for example modifiedBy field i actually get my data. Debugger log make me confused.
The call to forRevisionsOfEntityWithChanges returns an object array that contains:
Entity instance
Revision Entity
Revision Type
Property names that were changed.
How can i get REVINFO values ? 2 - Do i realy have to use entityManager, or can it be achived with different approach ?
So in your code, to get the revision info attributes, you would do the following. Note that in this code, the type of the revision-info object will depend on your configuration or if you're using a custom revision-info entity class in your deployment. Just be sure to cast it to the proper type.
for (Object entry : resultList) {
final Object[] row = (Object[]) entry;
final TheRevisionEntityClassType revisionInfo = row[1];
// now you can get the revision entity attributes from revisionInfo using getters
}
Correct me if i am wrong, but does forRevisionsOfEntityWithChanges works as Lazy Initialization? I mean, if i try to receive for example modifiedBy field i actually get my data. Debugger log make me confused.
Depending on the query, yes Hibernate may use proxies and its important to understand that in this case, the visual representation you get in the debugger may or may not be accurate depending if the object's internal state gets initialized by the debugger window or not.
I want an entity class through which I can store data to DB, for
specific columns I want in case-sensitive, is there any annotation to
be used above these specific columns to store as requested? please let
me if the question is not clear,Thanks in advance.
#Column(name = "EMAIL",unique=true) //i want store this email id as lower case,
private String email; //never mind in what case user enters
You can do this using #ColumnTransformer
#Column(name = "email")
#ColumnTransformer(write = "LOWER(email)")
private String email;
I try this and it works:
#Column
#ColumnTransformer(write = "LOWER(?)")
private String email;
expression of #ColumnTransformer.write would replace the original "?" placeholder in persisting sql statement. So must provide a compliant-sql expression with "?":
insert into member (email) values (?)
---->
insert into member (email) values (LOWER(?))
detail usage: https://docs.jboss.org/hibernate/orm/5.4/javadocs/org/hibernate/annotations/ColumnTransformer.html
I use Spring Boot and Spring Data for my project. In one of the tables I have a column called "itemStatus". Now I want to add another column with a timeStamp something like "itemStatus_LastModifiedDate" to keep the date of the latest update to the itemStatus column.
Is there a way to do this cleanly with the help of some annotations like:
#LastModifiedDate
#UpdateTimestamp
private Date itemStatusLastModifiedDate
Because as far I know these above only work when there is an update to the entity and not to a specific column.
1 You can not use annotation #UpdateTimestamp to track the update of a single field.
2 You can do a custom update using #SqlUpdate annotation with a custom update query.
Something like this (I didn't check this code)
#SqlUpdate(update item set item_status = :itemStatus, itemStatusModDate = case when (item_status = :itemStatus) then sysdate() else item_status_mod_date where id = :id)
public class Item {
#GeneratedValue
private Long id;
private String itemStatus;
private Date itemStatusModDate;
..................................................
}
I have an entity in Java and I would like Hibernate to create a foreign key from an Integer field (since I don't have an object reference):
#Entity
public class Invoice {
...
#Column(nullable = true)
private Integer generatedBy;
...
I think I'd like to do something like this with an attribute:
#ForeignKey(name="FK_Invoice_GeneratedBy", references="UserTable.UserId")
#Column(nullable = true)
private Integer generatedBy;
What is the best way to achieve this? I would preferably not have to maintain these relationships in a separate file (if possible).
There doesn't seem to be a solution to this, thus accepting this as an answer.
There is a way to do it, but it is not very nice...
You can have your integer attribute, AND an object attribute mapped this way:
#Column(ame = "GENERATED_BY", nullable = true)
private Integer generatedBy;
#ForeignKey(name="FK_Invoice_GeneratedBy")
#JoinColumn(name = "GENERATED_BY", nullable = false, updatable = false, insertable = false)
private User generatedByUser;
You may keep no external access to your generatedByUser field, it will only show hibernate that there is a relationship. You can set the Integer field at will, when you load this object from DB later you'll have your user reference.
Again, not very pretty, but can be useful sometimes.