So this save( ) returns primary key and will be persisted into database. But in this GORM it is not returning anything.
Db.Save(&empDTO) //Kindly check it is not returning primary key
Again if we returned this saved record to Service layer and tried to set value like empDTO.setEmail("john@hotmail.com") then Hibernate will automatically inserting this change into database by internally executing query which is not happening in this GORM.
So kindly asking you all I anybody knows how to deal with this , post your answer as I am facing this kind of difficulty in my current project
Another thing to know about GORM that is incredibly helpful, if you add the Debug() function in your chain it will spit the generated SQL into the console so you can see exactly what it is doing.
I think you’ll have to make a reproducible example, show your structs and your code. Create() is supposed to populate the primary key, and if it doesn’t do that for you we can work with your code to find out why.
By “reproducible example” I don’t mean loose snippets of your larger program, but a small test program that in itself shows the problem that we can run ourselves to see the behavior.
Yeah anything further we’d have to see your code to be honest. We don’t know what the employee struct you are using looks like, or if your fields are exported or not. I’m going to take a crack at it and say if you are hoping to assign that id to a long on the call, such as you have long id1 = (Long) session1.save(emp);
That won’t do in go, the id will be returned to the employee struct’s ID field.
Ohoo Thanks. I’ve updated my http://jinzhu.me/gorm/ library and it is working. Thanks a lot Mr.Jakob and Ms. CurtGreen.
Bdw Could you tell me as I’ve mentioned in my 1st discussion why that empDTO is not persisting.Like if saved object empDTO returned to Service Layer (MVC Architecture) and done some changes in empDTO e.g empDTO.setEmail("john@hotmail.com") then why it is not storing updated values to database by keeping empDTO in session by internally executing query. Hibernate does like this.Kindly reply because this one is the serious issue I’m facing.
package service
import (
"ZGolangBridge/dao"
"ZGolangBridge/model"
)
var UserDao dao.UserDAOImpl = dao.UserDAOImpl{}
type UserServiceImpl struct {
}
func (UService *UserServiceImpl) SaveUserInfo(userModel model.UserModel) model.UserModel {
var newUserModel model.UserModel
newUserModel = UserDao.SaveUser(userModel)
//newUserModel.SetEmail("martin@gmail.com") //change will not reflect to DB like happens in hibernate
/* Here if I set any value to model like newUserModel.SetEmail("martin@gmail.com") then
this updated value should reflect in database by internally executing update query and keeping
in session like what Hibernate query is doing. You know in Hibernate when object is saved by using
Session.save(UserModel) as I have given example in 1st discussion post ,then Hibernate keeps this UserModel object in persistent state and if we try to
do changes to it then it will automatically update the values to database. But like Hibernate GORM is
not supporting this by keeping object in session. For this we need to write seperate update query if we
try to do changes to object*/
return newUserModel
}
UserDAOImpl.go
package dao
import (
"ZGolangBridge/model"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
"log"
)
type UserDAOImpl struct {
}
var Db *gorm.DB
func DBConfig() {
var err error
Db, err = gorm.Open("mysql", "root:root@tcp(127.0.0.1:3306)/employeedb?charset=utf8&parseTime=True")
if err != nil {
log.Print(err)
return
}
Db.DropTableIfExists(&model.UserModel{})
Db.AutoMigrate(&model.UserModel{})
}
func (UDao *UserDAOImpl) SaveUser(userModel model.UserModel) model.UserModel {
Db.Save(&userModel)
return userModel
}
Kindly see the above code and revert back. If anything needed from my side,let me know.
func (UService *UserServiceImpl) SaveUserInfo(userModel model.UserModel) model.UserModel {
var newUserModel model.UserModel
newUserModel = UserDao.SaveUser(userModel)
//newUserModel.SetEmail("martin@gmail.com") //change will not reflect to DB like happens in hibernate
/* Here if I set any value to model like newUserModel.SetEmail("martin@gmail.com") then
this updated value should reflect in database by internally executing update query and keeping
in session like what Hibernate query is doing. You know in Hibernate when object is saved by using
Session.save(UserModel) then Hibernate keeps this UserModel object in persistent state and if we try to
do changes to it then it will automatically update the values to database. But like Hibernate GORM is
not supporting this by keeping object in session. For this we need to write seperate update query if we
try to do changes to object*/
return newUserModel
}
Calling newUserMode.SetEmail only modifies newUserModel. You have to save it to the DB yourself. Since the call to SaveUser happens before this call, the change is not saved to the DB because the code doesn’t do so.
Hello Mr.nathankerr, I agree with your point. Hibernate is doing the same so I asked this question in forum. Did you know any alternative to do like above ?
As far as I know, there is nothing like Hibernate for Go.
I think the main reason for this is that Hibernate needs to keep track of when things change, which means it needs to run code when a change is made. Go programs generally don’t use the accessor methods (e.g., SetEmail) that would be required to implement something like Hibernate.
While you could make this work by implementing the necessary logic in your DAO, I think it’s better to explicitly save to the DB.