The question here is why the entity properties are not being saved after calling some setters of the entity. Usually when changing a property of a managed entity, it should propagate to the database.
Take a look at this example:
#Service
public class SystemServiceImpl implements SystemService {
#Autowired
private SystemDao systemDao;
#Override
#Transactional
public System replace(Long systemID) {
// External system to replace
System system = systemDao.findByID(systemID);
if (null != system) {
system.setName("Test"); // Calling findByID again shows that this call did not have any effect.
}
return system;
}
}
-
#Entity
#Table(name = "db.system")
public class System {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long systemID;
private String name;
#OneToMany(mappedBy = "system", fetch = FetchType.LAZY)
#JsonIgnore
private List<Customer> customers = new ArrayList<Customer>();
public Long getSystemID() {
return systemID;
}
public void setSystemID(Long systemID) {
this.systemID = systemID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Customer> getCustomers() {
return customers;
}
public void setCustomers(List<Customer> customers) {
this.customers = customers;
}
}
If I call systemDao.merge after the system.setName("Test") then it is saved to the database. I feel I should not have to call merge as this should be a managed entity.
I tried having the replace method both with #Transactional and without, and both are producing the same result.
Any ideas?
Thanks,
The behaviour can be explained if SystemDao.findByID() returns a detached object. Make sure that SystemDao does not use RequiresNew-Transaction or explicitly detach the object after loading it.
It would really help if you posted the code of SystemDao and any relevant configuration entries (Spring, entity manager config, ...).
The error was happening when I was running my integration tests. Turns out that Transactional is not doing anything because there is no session. The solution to this was to add a Transactional to my tests.
Related
I am working with the Spring Boot Jpa Data Structure.
I have after successful test runs without a database been able to run my code flawlessly. After having hooked up my Spring Boot Application to a MySQL database and saving values into it, I run into the following issue:
Upon retrieving the data from database after restarting my application the List with the #ElementCollection is empty. I have checked the database and the data is present within the database, all other data is also retrieved without error. The application produces no error other than a Null Pointer which can be traced back to the value of the list being null.
Here is the code which is failing:
#Entity
public class Entity extends SuperEntity {
#ElementCollection
#LazyCollection(LazyCollectionOption.FALSE)
private List<String> flags;
public Entity() {
super();
this.flags = new LinkedList<>();
}
public Entity(List<String> flags) {
super();
this.flags = flags;
}
public Entity(#NotNull FormEntity formEntity) {
super();
this.flags = formEntity.getFlags();
}
public List<String> getFlags() {
return flags;
}
public void setFlags(List<String> flags) {
this.flags = flags;
}
}
#Entity
public abstract class SuperEntity {
protected #Id UUID id = UUID.randomUUID();
public SuperEntity() {}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
}
Any help is greatly appreciated and any additional materials needed from my end will be provided.
I've been using Spring Data for saving entities to the mongo DB and my code at the moment looks like this:
I have a repo class:
public interface LogRepo extends MongoRepository<Log, String> {
}
and I have an Entity Log which looks like this:
#Document(
collection = "logs"
)
public class Log {
#Id
private String id;
private String jsonMessage;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getJsonMessage() {
return jsonMessage;
}
public void setJsonMessage(String jsonMessage) {
this.jsonMessage = jsonMessage;
}
}
and this work well for me, however this approach works only for the case if I want to save Log entities to "logs" collection. However it would be very nice for me to be able to save Log entity to different collections depending on the context. I mean it would be nice to define collection name in the runtime. Is it possible somehow?
Thanks, cheers
Try to use inheritance and define appropriate collection names in such way. May give you possibility to save in different collections but you will be still not able to specify dynamically collection names and resp. their amount at runtime.
#Document(
collection = "logs"
)
public class Log {
#Id
private String id;
private String jsonMessage;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getJsonMessage() {
return jsonMessage;
}
public void setJsonMessage(String jsonMessage) {
this.jsonMessage = jsonMessage;
}
}
#Document(
collection = "log_child"
)
public class LogChild extends Log{}
With the MongoOperations save method you can choose which class to use and
based on the class it will choose the appropriate collection.
#Document(collection = "collection_#{T(com.github.your_project.DBUtils).getCollectionName()}")
public Class Collection
You can change the name in real time using a static getter
#UtilityClass
public class DBUtils {
private String collectionName;
public String getCollectionName() {
return collectionName;
}
public void setCollectionName(String collectionName) {
DBUtils.collectionName = collectionName;
}
}
I'm in the process of learning the Play Framework with the learn by doing approach. I'm trying to make a simple blog (using the information from the official site) and got stuck.
I'm trying to make a tree of comments of a post. So far I designed the model classes as follow:
The Post class:
#Entity
public class Post extends Model {
#Id
public Long id;
public String title;
public Date postedAt;
#Column(columnDefinition = "TEXT")
public String content;
#OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY, mappedBy="post")
public List<Comment> comments;
public static Finder<Long, Post> find = new Finder(Long.class, Post.class);
public static List<Post> all() {
return find.all();
}
public static void create(Post post) {
post.save();
}
public static void delete(Long id) {
find.ref(id).delete();
}
}
The Comment class:
#Entity
public class Comment extends Model {
#Id
public Long id;
public String content;
public static Finder<Long, Comment> find = new Finder(Long.class, Comment.class);
public static List<Comment> all() {
return find.all();
}
public static void create(Comment comment) {
comment.save();
}
public static void delete(Long id) {
find.ref(id).delete();
}
#OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY, mappedBy="post")
public List<ChildComment> childComments;
#ManyToOne
public Post post;
}
The ChildComment class:
public class ChildComment extends Model{
#Id
public Long id;
public String content;
#ManyToOne
public Comment comment;
}
The controller class Application.java
public class Application extends Controller {
public static Form<Post> postForm = Form.form(Post.class);
public static Result posts() {
return ok(views.html.index.render(Post.all(), postForm));
}
public static Result index() {
return redirect(routes.Application.posts());
}
public static Result newPost() {
Form<Post> filledForm = postForm.bindFromRequest();
if (filledForm.hasErrors()) {
return badRequest(views.html.index.render(Post.all(), filledForm));
} else {
Post.create(filledForm.get());
return redirect(routes.Application.posts());
}
}
public static Result deletePost(Long id) {
Post.delete(id);
return redirect(routes.Application.posts());
}
}
I know I have to use one-to many relationship to achieve the task (and in the model classes I think I did it correct) but I'm got stuck of the implementation of logic in controller to manage the comments and the comments of the comments. Any clue or advise will be great.
P.S. I'm using a MySql database
To create a comment you can do.
Pass post id and comment data in controller
Then in Controller
//post_id and commentData received from view
Post post=Post.findByPostId(post_id); //find post of that comment where findByPostId() is a function in model
Comment comment=new Comment(commentData,null,post); //Create a new Comment Object
Comment cm=Comment.save(comment); //where save() saves the Comment object in data base and return the saved object
List <Comments> allCommentsOnPost=post.getComments(); //get all comments on that post
allComments.add(cm); //add new comment to list
post.setComments(allCommentsOnPost); //set the new list in Post object
post.update(post_id); //update post entity
Similarly to save child comments pass comment_id,childCommentData from view
//comment_id and childCommentData received from view
Comment cm=Comment.findByCommentId(comment_id); //find comment from id ,findByCommentId() defined in Comment entity
ChildComment childCom=new ChildComment(childCommentData,cm); //create new object of ChildComment
ChildComment childComment=ChildComment.save(childCom); //persist the child comment object in db ,save() is a function in model which saves ChildComment object and return it
List<ChildComments> allChildComments=cm.getChildComments(); //getting list of all the ChildComments .
allChildComments.add(childComment); //add new comment to list
cm.setChildComments(allChildComments); //set all the child comments in Comment Entity
cm.update(comment_id); //update the Comments entity in db
Note:I am creating new Comment and ChildComment object in both the cases
respectively you can also use bindRequest().get() to get the Entity object
Actually you don't need to use two classes for children - this way you have to possibility of only two levels of comment, instead just add fields to your Comment model:
#ManyToOne
public Comment parent;
#OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY, mappedBy="parent")
public List<Comment> children;
This way you can have theoretically unlimited branches of tree.
during creating new comment your can add ID of the parent comment - if it's null, that means that the comment is on the root level (has no parents)
i have an entity with a unique name.
In my example i save two persons with the same name. At the second time comes an "EntityExists" (Unique) Exception, that was the expected behavior.
After it i changed name and set the "ID" to null.
Than i try to persist it again but i get "org.apache.openjpa.persistence.EntityExistsException: Attempt to persist detached object "com.Person#1117a20". If this is a new instance, make sure any version and/or auto-generated primary key fields are null/default when persisting.
without the version it works but i find no solution to "reset" the version number.
Can someone help me?
Update: My new problem is, that i have a base entity an two pcVersionInit (look at my answer at bottom) i can't override it, i tried it in base and normal entity what is the best practise now instead of "override" the value in pcVersionInit ? Copy Constructor?"
public class Starter{
private static EntityManager em;
public static void main(String[] args) {
em = Persistence.createEntityManagerFactory("openjpa")
.createEntityManager();
Person p1 = new Person("TEST");
savePerson(p1);
Person p2 = null;
try{
p2 = new Person("TEST");
savePerson(p2);
}catch(Exception e){
p2.setId(null);
p2.setName(p2.getName()+"2");
em.persist(p2);
}
}
private static void savePerson(Person person){
em.getTransaction().begin();
em.persist(person);
em.getTransaction().commit();
}
}
Person.class:
#Entity
public class Person implements Serializable{
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE,generator="seqGenerator")
#SequenceGenerator(name="seqGenerator",sequenceName="personSeq")
private Long id;
#Version
private Long version;
#Column(nullable=true, unique=true)
private String name;
public Person(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}
First off, stay away from messing around with pcVersionInit. I'd suggest to create a copy constructor in your Person Entity and in the event of rollback create a new one using the copy constructor.
Okay the problem is that OpenJPA adds a field named pcVersionInit (with #version) and set it "true" after try to persist. If i use reflection to set it to false, it works. Other way is a copy constructor.
I am writing a webservice to maintain a database. I am trying to use JPA (EclipseLink) for the entity classes. However, the database uses natural primary keys and therefore there's potential that an update on the ID fields will fail due to foreign key constraints. Our DBA has provided a function to update the ID fields which will create a new parent record with the updated ID, update the child records to point to the new parent and delete the old parent.
If the ID fields could be updated "normally", I would have a situation like this:
#Entity
#Table(name = "PARENT")
public class Parent implements Serializable
{
private static final long serialVersionUID = 1L;
private String parent;
private String attribute;
private Set<Child> childs;
public Parent()
{
}
#Id
#Column(name = "PARENT")
public String getParent()
{
return this.parent;
}
public void setParent(String parent)
{
this.parent = parent;
}
#Column(name = "ATTRIBUTE")
public String getAttribute()
{
return this.attribute;
}
public void setAttribute(String attribute)
{
this.attribute = attribute;
}
#OneToMany(mappedBy = "parentBean")
public Set<Child> getChilds()
{
return this.childs;
}
public void setChilds(Set<Child> childs)
{
this.childs = childs;
}
}
#Entity
#Table(name = "CHILD")
public class Child implements Serializable
{
private static final long serialVersionUID = 1L;
private String child;
private String attribute;
private Parent parentBean;
public Child()
{
}
#Id
#Column(name = "CHILD")
public String getChild()
{
return this.child;
}
public void setChild(String child)
{
this.child = child;
}
#Column(name = "ATTRIBUTE")
public String getAttribute()
{
return this.attribute;
}
public void setAttribute(String attribute)
{
this.attribute = attribute;
}
#ManyToOne
#JoinColumn(name = "PARENT")
public Parent getParent()
{
return this.parent;
}
public void setParent(Parent parent)
{
this.parent = parent;
}
}
I also have a GenericServiceBean class with a method to call functions:
#Stateless
public class GenericServiceBean implements GenericService
{
#PersistenceContext(unitName = "PersistenceUnit")
EntityManager em;
public GenericServiceBean()
{
// empty
}
#Override
public <T> T create(T t)
{
em.persist(t);
return t;
}
#Override
public <T> void delete(T t)
{
t = em.merge(t);
em.remove(t);
}
#Override
public <T> T update(T t)
{
return em.merge(t);
}
#Override
public <T> T find(Class<T> type, Object id)
{
return em.find(type, id);
}
. . .
#Override
public String executeStoredFunctionWithNamedArguments(String functionName,
LinkedHashMap<String, String> namedArguments)
{
Session session = JpaHelper.getEntityManager(em).getServerSession();
StoredFunctionCall functionCall = new StoredFunctionCall();
functionCall.setProcedureName(functionName);
functionCall.setResult("RESULT", String.class);
for (String key : namedArguments.keySet())
{
functionCall.addNamedArgumentValue(key, namedArguments.get(key));
}
ValueReadQuery query = new ValueReadQuery();
query.setCall(functionCall);
String status = (String)session.executeQuery(query);
return status;
}
}
If I set the ID fields to be not editable:
#Id
#Column(name = "PARENT", udpatable=false)
public String getParent()
{
return this.parent;
}
and call parent.setParent(newParent) will this still update the ID in the entity object? How does this affect any child entities? Will they also be updated (or not)?
Another scenario I don't know how to deal with is where I need to update both the ID and another attribute. Should I call the function which updates (and commits) the ID in the database then make calls to set both the ID and attribute via the normal set* methods and then the persistence context will only commit the attribute change?
Perhaps this is a situation where JPA is not appropriate?
Any advice on this is greatly appreciated.
If I set the ID fields to be not editable (...) and call parent.setParent(newParent) will this still update the ID in the entity object? How does this affect any child entities? Will they also be updated (or not)?
updatable=false means that the column won't be part of the SQL UPDATE statement regardless of what you do at the object level so the Id shouldn't be updated. And I'm also tempted to say that child entities shouldn't be affected, especially since you're not cascading anything.
Another scenario I don't know how to deal with is where I need to update both the ID and another attribute (...)
Well, my understanding is that you'd have to call the function anyway so I would call it first.
Perhaps this is a situation where JPA is not appropriate?
I'm not sure raw SQL would deal better with your situation. Actually, the whole idea of changing primary keys sounds strange if I may.