I have 2 tables I want to join on PK and FK in JPA, but the PK is upper case and the FK lower case.
How do I map case insensitive association between Person -> GroupAssociationEntity?
My current mapping is not working.
#Entity
public class Person {
#Id
#Column(name = "id", columnDefinition = "nvarchar")
private String id;
#OneToMany(mappedBy = "person")
private List<GroupAssociationEntity> groups;
}
#Entity
#IdClass(GroupAssociationKey.class)
public class GroupAssociationEntity {
#Id
private String id;
#Id
private String memberOf;
#ManyToOne
#JoinColumn(name = "id", updatable = false, insertable = false, referencedColumnName = "id")
private Group group;
#ManyToOne
#JoinColumn(name = "memberOf", updatable = false, insertable = false, referencedColumnName = "id")
private Person person;
....
}
#Entity
public class Group {
#Id
#Column(name = "id")
private String id;
#OneToMany(mappedBy = "group")
private List<GroupAssociationEntity> persons;
......
}
I switched your mapping to:
#Entity(name = "Person")
public static class Person {
#Id
#Column(name = "id")
private String id;
#OneToMany(mappedBy = "person")
private List<GroupAssociationEntity> groups;
}
#Entity(name = "GroupAssociationEntity")
public static class GroupAssociationEntity {
#EmbeddedId
private GroupAssociationKey id;
#ManyToOne
#MapsId("id")
private Group group;
#ManyToOne
#MapsId("memberOf")
private Person person;
}
#Embeddable
public static class GroupAssociationKey implements Serializable{
private String id;
private String memberOf;
public GroupAssociationKey() {
}
public GroupAssociationKey(String id, String memberOf) {
this.id = id;
this.memberOf = memberOf;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMemberOf() {
return memberOf;
}
public void setMemberOf(String memberOf) {
this.memberOf = memberOf;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof GroupAssociationKey)) return false;
GroupAssociationKey that = (GroupAssociationKey) o;
return Objects.equals(getId(), that.getId()) &&
Objects.equals(getMemberOf(), that.getMemberOf());
}
#Override
public int hashCode() {
return Objects.hash(getId(), getMemberOf());
}
}
#Entity(name = "Group")
#Table(name = "groups")
public static class Group {
#Id
#Column(name = "id")
private String id;
#OneToMany(mappedBy = "group")
private List<GroupAssociationEntity> persons;
}
And run this test on both SQL Server and MySQL:
doInJPA( entityManager -> {
Person person1 = new Person();
person1.id = "abc1";
entityManager.persist(person1);
Person person2 = new Person();
person2.id = "abc2";
entityManager.persist(person2);
Group group = new Group();
group.id = "g1";
entityManager.persist(group);
GroupAssociationEntity p1g1 = new GroupAssociationEntity();
p1g1.id = new GroupAssociationKey("G1", "ABC1");
p1g1.group = group;
p1g1.person = person1;
entityManager.persist(p1g1);
GroupAssociationEntity p2g1 = new GroupAssociationEntity();
p2g1.id = new GroupAssociationKey( "G1", "ABC2" );
p2g1.group = group;
p2g1.person = person2;
entityManager.persist(p2g1);
} );
doInJPA( entityManager -> {
Group group = entityManager.find(Group.class, "g1");
assertEquals(2, group.persons.size());
} );
doInJPA( entityManager -> {
Person person = entityManager.find(Person.class, "abc1");
assertEquals(1, person.groups.size());
} );
And it works just fine. Check it out on GitHub.
#Entity
public class Person {
#Id
#Column(name = "id", columnDefinition = "nvarchar")
private String id;
}
#Entity
#IdClass(GroupAssociationKey.class)
public class GroupAssociationEntity {
#Id
private String id;
#Id
private String memberOf;
#ManyToOne
#JoinColumn(name = "id", updatable = false, insertable = false, referencedColumnName = "id")
private Group group;
#ManyToOne
#JoinColumn(name = "id", updatable = false, insertable = false, referencedColumnName = "id")
private Person person;
....
}
#Entity
public class Group {
#Id
#Column(name = "id")
private String id;
}
try this.
Related
I want to return a Profile Object in JSON containing a list of login details associated with a social network.
Everything works correctly when the "reseaux_sociaux" table is empty. For my status table I get my statuses in JSON format in my Profile object. However, when "reseaux_sociaux" contains values then I get the error below and my Profile object in JSON format is not returned...
(Logs)
https://cloudvyzor.com/logpad/?query&database=sandbox-7fb06b2c06f198a7c0e4ff7c74d659e0
Profil Class
#Entity
#Table(name = "Profil")
public class Profil {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long Id;
#ManyToOne(fetch = FetchType.LAZY, optional = false)
#JoinColumn(name = "IdComptes", nullable = false)
private Comptes IdComptes;
private String Avatar;
private String Banniere;
private String Pseudo;
private String MailPro;
private String Bio;
#ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
#JoinTable(name = "Statut_Profil", joinColumns = #JoinColumn(referencedColumnName = "Id"),inverseJoinColumns = #JoinColumn(referencedColumnName ="Id"))
private List<Statut> Statut;
#OneToMany( mappedBy = "IdProfil")
#JsonManagedReference("id_profil")
private List<ReseauxSociaux> Reseaux;
public Profil(){}
public Profil(Long id, Comptes idComptes, String avatar, String banniere, String pseudo, String mailPro, String bio) {
Id = id;
IdComptes = idComptes;
Avatar = avatar;
Banniere = banniere;
Pseudo = pseudo;
MailPro = mailPro;
Bio = bio;
}
}
ReseauxSociaux Class
#Entity
#IdClass(ReseauxId.class)
public class ReseauxSociaux {
#Id
private int Id;
#Id
private Long IdProfil;
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "IdProfil", insertable = false, updatable = false)
#JsonBackReference("id_profil")
private Profil Profil;
#ManyToOne
#JoinColumn(name = "Id", insertable = false, updatable = false)
#JsonBackReference("id")
private Reseau Reseau;
private String Identifiant;
private ReseauxSociaux()
{}
public ReseauxSociaux(int id, Long idProfil, String identifiant) {
Id = id;
IdProfil = idProfil;
Identifiant = identifiant;
}
}
Reseau class
#Entity
public class Reseau {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int Id;
private String Nom;
private String Couleur;
//I tried it with and without and it made no difference
#OneToMany( mappedBy = "Id")
#JsonManagedReference("id")
private List<ReseauxSociaux> Reseaux;
public Reseau(){}
public Reseau(int id, String nom, String couleur) {
Id = id;
Nom = nom;
Couleur = couleur;
}
//Get Set
}
Profil Controller
#RestController
#RequestMapping("/profil")
public class ProfilController {
private final ProfilRepository profilRepository;
public ProfilController(ProfilRepository profilRepository) {
this.profilRepository = profilRepository;
}
#PostMapping("/getprofil/{idCompte}")
Profil GetProfil(#PathVariable("idCompte") Long idCompte)
{
Profil profil= profilRepository.findProfilById(idCompte);
return profil;
}
}
I finally succeeded... The cause of the problem remains unclear but I have two hypotheses: the first is the use of capital letters on variable names; the second is the use of a list with the onetomany with the same name in two entities.
Profil class
#Entity
#Table(name = "Profil")
public class Profil {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#ManyToOne(fetch = FetchType.LAZY, optional = false)
#JoinColumn(name = "idComptes", nullable = false)
#JsonBackReference("id_comptes")
private Comptes idComptes;
private String avatar;
private String banniere;
private String pseudo;
private String mailPro;
private String bio;
#ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
#JoinTable(name = "statut_profil", joinColumns = #JoinColumn(referencedColumnName = "id"),inverseJoinColumns = #JoinColumn(referencedColumnName ="id"))
private List<Statut> statut;
#OneToMany( mappedBy = "idProfil")
#JsonManagedReference("id_profil")
private List<ReseauxSociaux> lstprofil;
public Profil(){}
public Profil(Long id, Comptes idComptes, String avatar, String banniere, String pseudo, String mailPro, String bio) {
this.id = id;
this.idComptes = idComptes;
this.avatar = avatar;
this.banniere = banniere;
this.pseudo = pseudo;
this.mailPro = mailPro;
this.bio = bio;
}
//get set
}
ReseauxSociaux class
#Entity
#IdClass(ReseauxId.class)
public class ReseauxSociaux {
#Id
private int id;
#ManyToOne
#JoinColumn(name = "id", insertable = false, updatable = false)
#JsonBackReference("id_reseau")
private Reseau reseau;
#Id
private Long idProfil;
#ManyToOne
#JoinColumn(name = "idProfil", insertable = false, updatable = false)
#JsonBackReference("id_profil")
private Profil profil;
private String identifiant;
private ReseauxSociaux()
{}
public ReseauxSociaux(int id, Reseau reseau, Long idProfil, String identifiant) {
this.id = id;
this.reseau = reseau;
this.idProfil = idProfil;
this.identifiant = identifiant;
}
}
Reseau class
#Entity
public class Reseau {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String nom;
private String couleur;
#OneToMany( mappedBy = "id")
#JsonManagedReference("id_reseau")
private List<ReseauxSociaux> lstreseau;
public Reseau(){}
public Reseau(int id, String nom, String couleur) {
this.id = id;
this.nom = nom;
this.couleur = couleur;
}
}
I've a bidirectional relationship between two entities: Item (15 records) and maintenanceContract (22 records)
Mapping table item_maintenanceContract linking these two entities contains 25 records
I would like to count the records in the maintenanceContract table and I run the following query:
"SELECT count(*) FROM MaintenanceContract mc LEFT JOIN mc.items as i "
Unfortunately I visualize 25 instead of the desidered 22.
I cannot simply count on MaintenanceContract because join with Item is required when i perform same searches based on item attributes.
I tried alternative combinations: LEFT JOIN , left outer JOIN, right JOIN... no changes in the result
What I'm missing?
My item definition:
#Entity
#Table(name = "item"})
#Inheritance(strategy = InheritanceType.JOINED)
public class Item extends BaseEntity implements Serializable {
private Long id;
private Set<MaintenanceContract> maintenanceContracts = new HashSet<MaintenanceContract>(0);
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "id", unique = true, nullable = false)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
#ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
#JoinTable(name = "item_maintenancecontract", joinColumns = {
#JoinColumn(name = "item_id", nullable = false, updatable = false)},
inverseJoinColumns = {#JoinColumn(name = "maintenancecontract_id",
nullable = false, updatable = false)})
public Set<MaintenanceContract> getMaintenanceContracts() {
return maintenanceContracts;
}
public void setMaintenanceContracts(Set<MaintenanceContract> maintenanceContracts) {
this.maintenanceContracts = maintenanceContracts;
}
}
and my MaintenanceContract
#Entity
public class MaintenanceContract implements Serializable {
private Long id;
private Set<Item> items = new HashSet<Item>(0);
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#ManyToMany(fetch = FetchType.EAGER, mappedBy = "maintenanceContracts")
public Set<Item> getItems() {
return this.items;
}
public void setItems(Set<Item> items) {
this.items = items;
}
}
I've been trying to get my head around how I can create a common tag asset library with hibernate, but I can't get it to work.
I want it to look behave something like this:
I want Subscription and Notification to share the same unique tag library.
I have tried with a #ManyToMany annotation. But I don't think that's the way to do it. In my best case scenario I want Hibernate to automatically see if a tag is already present, and then just use that id. But I'm also okay with first creating tags and then linking them. How is the best way to go about this with Hibernate?
Here's my current code:
Subscription.class
#Data
#Entity
public class Subscription {
#Id
#GeneratedValue
#Column(name = "subscription_id")
private Long id;
#ManyToMany(cascade = CascadeType.ALL)
#JoinTable(name = "subscription_tag",
joinColumns = { #JoinColumn(name = "subscription_id") },
inverseJoinColumns = { #JoinColumn(name = "tag_id") })
private List<Tags> tags = new ArrayList<>();
private String subscriberString;
}
Tags.class
#Data
#Entity
#Table(
name = "notification_tags",
uniqueConstraints = #UniqueConstraint(columnNames = "tag")
)
public class Tags {
#Id
#GeneratedValue
#Column(name = "tag_id")
private Long id;
private String tag;
}
Notification.class
#Data
#Entity(name = "Notification")
#Table(name = "notification")
#TypeDef(
name = "jsonb-node",
typeClass = JsonNodeStringType.class
)
public class Notification extends NotificationBase {
#Id
#GeneratedValue
#Column(name = "notification_id")
private Long id;
// This is when the object was created
private Date updatedTime = new Date();
private Date timestamp;
private String name;
#ManyToMany(cascade = CascadeType.ALL)
#JoinTable(name = "notification_tag",
joinColumns = { #JoinColumn(name = "notification_id") },
inverseJoinColumns = { #JoinColumn(name = "tag_id") })
private List<Tags> tags = new ArrayList<>();
#Type(type = "jsonb-node")
#Column(columnDefinition = "NVARCHAR(4000)")
private JsonNode customJson;
}
I had some relations wrong. You should only have one Class own the relation, but the other class should reference back to that relation.
I also used #JsonManagedReference and #JsonBackReference to avoid circular referencing when Serializing to json.
When I want to search for all Subscriptions with a tag I did not need to search for the tag that is in all Subscriptions, but the other way around, all Subscriptions with a certain tag. See my code below.
SubscriptionRepository.class (to find all Subscription with a certain tag)
public interface SubscriptionRepository extends CrudRepository<Subscription, Long>{
List<Subscription> findAllByTag(Tag tag);
}
Subscription.class
#Data
#Entity
public class Subscription {
#Id
#GeneratedValue
#Column(name = "subscription_id")
private Long id;
#ManyToMany(cascade = CascadeType.ALL)
#JoinTable(name = "subscription_tag",
joinColumns = { #JoinColumn(name = "subscription_id") },
inverseJoinColumns = { #JoinColumn(name = "tag_id") })
private Set<Tags> tags = new HashSet<>();
private String subscriberString;
}
Tag.class
#Data
#Entity
#Table(
name = "notification_tags",
uniqueConstraints = #UniqueConstraint(columnNames = "tag")
)
public class Tag {
#Id
#GeneratedValue
#Column(name = "tag_id")
private Long id;
private String tag;
#ManyToMany(mappedBy = "tags", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
#JsonBackReference
private Set<Subscription> subscriptions = new HashSet<>();
#ManyToMany(mappedBy = "tags", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
#JsonBackReference
private Set<Notification> notifications = new HashSet<>();
public Tag() {
}
#Builder
public Tag(String tag) {
this.tag = tag;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Tag tag1 = (Tag) o;
return Objects.equals(tag, tag1.tag);
}
#Override
public int hashCode() {
return Objects.hash(super.hashCode(), tag);
}
}
Notification.class
#Data
#Entity(name = "Notification")
#Table(name = "notification")
#TypeDef(
name = "jsonb-node",
typeClass = JsonNodeStringType.class
)
public class Notification extends NotificationBase {
#Id
#GeneratedValue
#Column(name = "notification_id")
private Long id;
// This is when the object was created
private Date updatedTime = new Date();
private Date timestamp;
private String name;
#ManyToMany(cascade = CascadeType.ALL)
#JoinTable(name = "notification_tag",
joinColumns = { #JoinColumn(name = "notification_id") },
inverseJoinColumns = { #JoinColumn(name = "tag_id") })
private Set<Tags> tags = new HashSet<>();
#Type(type = "jsonb-node")
#Column(columnDefinition = "NVARCHAR(4000)")
private JsonNode customJson;
}
I would like to sovle this problec, cause it's looks interesting, to get grouped objects directly from box
I have db sheme about this:
Teacher
-id
-name
Group
-id
-name
Subject
-id
-name
Several teachers can teaches one subject
Subject_teachers
-subject_id
-teacher_id
Group_subjects
-group_id
-subject_id
I would to get grouped lessons by groups for teacher
class Teacher{
#Id id ,
...
#ManyToMany
???
Map<Group,Subject) subjects;
};
#Entity
#Table(name = "Teacher")
public class Teacher {
private Integer teacherId;
private String name;
private Set<SubjectTeachers> subjectTeachers = new HashSet<SubjectTeachers>(0);
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "teacher_id", unique = true, nullable = false)
public Integer getTeacherId() {
return teacherId;
}
public void setTeacherId(Integer teacherId) {
this.teacherId = teacherId;
}
#Column(name = "name", nullable = false)
public String getName() {
return name;
}
public void setTitle(String name) {
this.name = name;
}
#OneToMany(mappedBy = "subjectTeachers", cascade = CascadeType.ALL, fetch=FetchType.LAZY)
public Set<SubjectTeachers> getSubjectTeachers() {
return subjectTeachers;
}
public void setProjectTags(Set<SubjectTeachers> subjectTeachers) {
this.subjectTeachers = subjectTeachers;
}
}
#Entity
#Table(name = "Subject")
public class Subject{
private Integer subjectId;
private String name;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "subject_id", unique = true, nullable = false)
public Integer getSubjectId() {
return subjectId;
}
public void setSubjectId(Integer subjectId) {
this.subjectId = subjectId;
}
#Column(name = "name", nullable = false)
public String getName() {
return name;
}
public void setTitle(String name) {
this.name = name;
}
}
#Entity
#Table(name ="subject_teachers")
public SubjectTeachers{
private Integer subjectTeachersID;
private Teacher teacher;
private Subject subject;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "subjectTeachersID", unique = true, nullable = false)
public Integer getSubjectTeachersId() {
return subjectTeachersID;
}
public void setSubjectTeachersId(Integer subjectTeachersID) {
this.subjectTeachersID = subjectTeachersID;
}
#ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
#JoinColumn(name = "teacher_id", nullable = false)
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
#OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
#JoinColumn(name = "subject_id", nullable = false)
public Subject getSubject() {
return subject;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
}
#Entity
#Table(name = "group_subjects")
public class GroupSubjects{
private Integer groupId;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "group_id", unique = true, nullable = false)
public Integer getGroupId() {
return groupId;
}
public void setGroupId(Integer groupId) {
this.groupId = groupId;
}
private Subject subject;
#ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
#JoinColumn(name = "subject_id", nullable = false)
public Subject getSubject() {
return subject;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
}
Hope its help to you..
I try to gather statistics of visitors for two services. It consists of daily visitors statistics and overall record. Each service can be accessed by different names. For example, user, admin, support etc. Each will have its own record as own statistics.
Here is my DB structure:
service_one: id, name
service_two: id, name
daily_stats: id, date, service_one_id, service_one_visitors,
service_two_id, service_two_visitors, overall_visitors
record_stats: id, service_one_id, service_one_record,
service_one_record_date, service_two_id, service_two_record,
service_two_record_date
Here are the relations between tables:
service_one --- (one to many) ---> daily_stats(service_one_id)
service_one --- (one to many) ---> record_stats(service_one_id)
service_two --- (one to many) ---> daily_stats(service_two_id)
service_two --- (one to many) ---> record_stats(service_two_id)
Mapping for service_one (the same is for service_two). Also setters were omitted in order to shorten the example:
#Entity
#Table(name = "service_one")
public class ServiceOne implements Serializable {
private int id;
private String name;
private Set<RecordStats> recordStats = new HashSet<RecordStats>(0);
private Set<DailyStats> dailyStats = new HashSet<DailyStats>(0);
public ServiceOne() {}
public ServiceOne(int id, String name) {
this.id = id;
this.name = name;
}
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "id", nullable = false, unique = true)
public int getId() {
return id;
}
#Column(name = "name")
public String getName() {
return name;
}
#OneToMany(fetch = LAZY, mappedBy = "service_one_id")
public Set<RecordStats> getRecordStats() {
return recordStats;
}
#OneToMany(fetch = LAZY, mappedBy = "service_one_id")
public Set<DailyStats> getDailyStats() {
return dailyStats;
}
}
daily_stats mapping:
#Entity
#Table(name = "daily_stats", uniqueConstraints = {
#UniqueConstraint(columnNames = "date")
})
public class DailyStats implements Serializable{
private int id;
private Date date;
private ServiceOne service_one_id;
private int service_one_visitors;
private ServiceTwo service_two_id;
private int service_two_visitors;
private int overall_visitors;
public DailyStats() {}
public DailyStats(DailyStats rec) {
this.id = rec.getId();
//...
}
#Id
#GeneratedValue
#Column(name = "id", nullable = false)
public int getId() {
return id;
}
#Temporal(DATE)
#Column(name = "date")
public Date getDate() {
return date;
}
#ManyToOne(fetch = LAZY)
#JoinColumn(name = "id", nullable = false)
public ServiceOne getService_one_id() {
return service_one_id;
}
#Column(name = "service_one_visitors")
public int getService_one_visitors() {
return service_one_visitors;
}
#ManyToOne(fetch = LAZY)
#JoinColumn(name = "id", nullable = false)
public ServiceTwo getService_two_id() {
return service_two_id;
}
#Column(name = "service_two_visitors")
public int getService_two_visitors() {
return service_two_visitors;
}
#Column(name = "overall_visitors")
public int getOverall_visitors() {
return overall_visitors;
}
}
record_stats mapping:
#Entity
#Table(name = "record_stats", uniqueConstraints = {
#UniqueConstraint(columnNames = "service_one_record_date"),
#UniqueConstraint(columnNames = "service_two_record_date")
})
public class RecordStats implements Serializable {
private int id;
private ServiceOne service_one_id;
private int service_one_record;
private Date service_one_rec_date;
private ServiceTwo service_two_id;
private int service_two_record;
private Date service_two_rec_date;
public RecordStats() {}
public RecordStats(RecordStats rec) {
this.id = rec.getId();
//...
}
#Id
#GeneratedValue
#Column(name = "id", nullable = false)
public int getId() {
return id;
}
#ManyToOne(fetch = LAZY)
#JoinColumn(name = "id", nullable = false)
public ServiceOne getService_one_id() {
return service_one_id;
}
#Column(name = "service_one_record")
public int getService_one_record() {
return service_one_record;
}
#Column(name = "service_one_record_date")
#Temporal(DATE)
public Date getService_one_rec_date() {
return service_one_rec_date;
}
#ManyToOne(fetch = LAZY)
#JoinColumn(name = "id", nullable = false)
public ServiceTwo getService_two_id() {
return service_two_id;
}
#Column(name = "service_two_record")
public int getService_two_record() {
return service_two_record;
}
#Column(name = "service_two_record_date")
#Temporal(DATE)
public Date getService_two_rec_date() {
return service_two_rec_date;
}
}
Trying to create new entry throws exception:
public static void main(String[] args) {
ServiceOne serviceOne = new ServiceOne();
serviceOne.setName("test");
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(serviceOne);
session.getTransaction().commit();
//get records
session = sessionFactory.openSession();
session.beginTransaction();
List result = session.createQuery("from service_one").list();
for (ServiceOne o : (List<ServiceOne>)result) {
System.out.println(o.getName());
}
session.getTransaction().commit();
session.close();
}
org.hibernate.MappingException: Repeated column in mapping for entity:
VisitorsCounter.model.entity.DailyStats column: id (should be
mapped with insert="false" update="false")
What is wrong with my mapping?
It seems to me that
#JoinColumn(name = "id", nullable = false)
public ServiceOne getService_one_id() {
return service_one_id;
}
in DailyStats is wrong; you should have name = "service_one_id".
You have the same problem in getService_two_id() and in methods of same names in RecordStats.
May I also ask why don't you name the references in the classes fields serviceOne and serviceTwo instead of service_one_id and service_two_id.