2

I work with a Spring Boot project and I have the following entities,

@Entity
public class Ip {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "IP_ADDR_ID_2")
    private Long id;

    @Column(name = "IP_ADDRESS_2")
    @NotEmpty
    private String address;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "ip")
    private List<IMAssociation> httpInfoMessages2;

}

The message entity is provided below,

@Entity
public class Message {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "S_ID_2")
    private Long sId;

    @Column(name = "STATUS_ID_2")
    private Long statusId;

    @NotEmpty
    @Column(name = "STATUS_2")
    private String status;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "message")
    private List<IMAssociation> message;
}

And, lastly, the entity make the association between the Ip and the Message is provided,

@Entity
@Table(name = "IP_HTTP_MESSAGE")
//@IdClass(AssociationId.class)
public class IMAssociation implements Serializable{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Id
    @Column(name = "IP_ADDRESS_ID")
    private Long ipAddressId;

    @Id
    @Column(name = "HTTP_MESSAGE_ID")
    private Long httpMessageId;

    @ManyToOne
    @PrimaryKeyJoinColumn(name = "ipAddressId", referencedColumnName = "IP_ADDR_ID_2")
    //    @JoinColumn(name = "ipAddressId", updatable = false, insertable = false, referencedColumnName = "IP_ADDR_ID_2")
    private Ip ip;

    @ManyToOne
    @PrimaryKeyJoinColumn(name = "httpMessageId", referencedColumnName = "S_ID_2")
    //    @JoinColumn(name = "httpMessageId", updatable = false, insertable = false, referencedColumnName = "S_ID_2")
    private Message message;


//    @ManyToOne
    @JoinColumn(name = "IP_ADDRESS_2", updatable = false, insertable = false, referencedColumnName = "IP_ADDRESS_2")
    private String address;

    @JoinColumn(name = "STATUS_CODE", updatable = false, insertable = false, referencedColumnName = "STATUS_ID_2")
    private Long code;

    @JoinColumn(name = "STATUS_MESSAGE", updatable = false, insertable = false, referencedColumnName = "STATUS_2")
    private String codeMessage;


    public IMAssociation() {
    }

    public IMAssociation(Long code, String codeMessage) {
        this.code = code;
        this.codeMessage = codeMessage;
    }

}

The List<IMAssociation> is made therough the code,

List<IMAssociation> imAssociations = new ArrayList<>();

for(int j =0; j < 5; j++){

     IMAssociation imAssociation = new IMAssociation(Long.valueOf(integerListEntry.getKey()),
                                            message);
    imAssociations.add(imAssociation);
}


Ip ip = new Ip('127.0.0.1', imAssociations); 

List<Ip> ips = new ArrayList<>();
ips.add(ip);

When I tried to persist a List<Ip> in the database, I get the following error,

Caused by: org.springframework.orm.jpa.JpaSystemException: Could not set field value [POST_INSERT_INDICATOR] value by reflection : [class com.ef.entity.IMAssociation.id] setter of com.ef.entity.IMAssociation.id; nested exception is org.hibernate.PropertyAccessException: Could not set field value [POST_INSERT_INDICATOR] value by reflection : [class com.ef.entity.IMAssociation.id] setter of com.ef.entity.IMAssociation.id


Caused by: org.hibernate.PropertyAccessException: Could not set field value [POST_INSERT_INDICATOR] value by reflection : [class com.ef.entity.IMAssociation.id] setter of com.ef.entity.IMAssociation.id
    at org.hibernate.property.access.spi.SetterFieldImpl.set(SetterFieldImpl.java:58) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.mapping.Component$ValueGenerationPlan.execute(Component.java:419) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.id.CompositeNestedGeneratedValueGenerator.generate(CompositeNestedGeneratedValueGenerator.java:97) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:765) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:758) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener$1.cascade(JpaPersistEventListener.java:80) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:398) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:162) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:431) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:363) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:326) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:162) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:111) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:278) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:178) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:109) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:775) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:748) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:753) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1146) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]


Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Long field com.ef.entity.IMAssociation.id to org.hibernate.id.IdentifierGeneratorHelper$2
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) ~[na:1.8.0_144]
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) ~[na:1.8.0_144]
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81) ~[na:1.8.0_144]

What is the issue here?

Update

I have changed the IMAssociation class and its provided below,

@Entity
@Table(name = "IP_HTTP_MESSAGE")
//@IdClass(AssociationId.class)
public class IMAssociation implements Serializable {

//    @Id
//    @GeneratedValue(strategy = GenerationType.IDENTITY)
//    private Long id;

    @Id
    @Column(name = "IP_ADDRESS_ID")
    private Long ipAddressId;

    @Id
    @Column(name = "HTTP_MESSAGE_ID")
    private Long httpMessageId;

    @ManyToOne
    @PrimaryKeyJoinColumn(name = "IP_ADDRESS_ID", referencedColumnName = "IP_ADDR_ID_2")
//    @JoinColumn(name = "ipAddressId", updatable = false, insertable = false, referencedColumnName = "IP_ADDR_ID_2")
    private Ip ip;

    @ManyToOne
    @PrimaryKeyJoinColumn(name = "HTTP_MESSAGE_ID", referencedColumnName = "S_ID_2")
//    @JoinColumn(name = "httpMessageId", updatable = false, insertable = false, referencedColumnName = "S_ID_2")
    private Message message;


    //    @ManyToOne
    @JoinColumn(name = "IP_ADDRESS_2", updatable = false, insertable = false, referencedColumnName = "IP_ADDRESS_2")
    private String address;

    @JoinColumn(name = "STATUS_CODE", updatable = false, insertable = false, referencedColumnName = "STATUS_ID_2")
    private Long code;

    @JoinColumn(name = "STATUS_MESSAGE", updatable = false, insertable = false, referencedColumnName = "STATUS_2")
    private String codeMessage;


    public IMAssociation() {
    }

    public IMAssociation(Long code, String codeMessage) {
        this.code = code;
        this.codeMessage = codeMessage;
    }
}

Now, I get the following error while I run the app,

Caused by: javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [com.ef.entity.IMAssociation#IMAssociation{, ipAddressId=null, httpMessageId=null, ip=null, message=null, address='null', code=404, codeMessage='404_Not Found'}]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1664) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1608) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1152) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]
Arefe
  • 11,321
  • 18
  • 114
  • 168
  • Please have a look at https://stackoverflow.com/questions/16124638/what-cause-org-hibernate-propertyaccessexception-exception-occurred-inside-sett –  Oct 14 '17 at 03:42
  • You have multiple primary keys in your entity `IMAssociation`. You either declare an `@IdClass` or an `@EmbeddedId` for having compound primary keys/composite keys, but I don't see you have used any of these. – Ish Oct 14 '17 at 04:20
  • 1
    Also, you are incorrectly using `@PrimaryKeyJoinColumn` here. The attribute `name` should map to the physical column name of your table and not some entity property. So it should be `@PrimaryKeyColumn(name="IP_ADDRESS_ID", referencedColumnName = "IP_ADDR_ID_2")` and `@PrimaryKeyColumn(name="HTTP_MESSAGE_ID", referencedColumnName = "S_ID_2")` – Ish Oct 14 '17 at 04:26
  • I have tried with `@IdClass(AssociationId.class)` in the top of the `Ip` class where `AssociationId` implements the `Serializable`. This doesn't solve the issue and still provides the same error. Whats the correct way to use the `@EmbeddedId`? I have used it instead of the `@Id` and gets some error – Arefe Oct 14 '17 at 04:29
  • @Ish I have actually tried all suggestions I provided and still gets the same error. Would you mind to provide a working solution if you have an open IDE now? – Arefe Oct 14 '17 at 04:33
  • @AAA, I'm trying to replicate your setup, might take time though. – Ish Oct 14 '17 at 05:04
  • @Ish I suggest starting a `Spring Boot` project, connect with the db with `application.properites` and copy/paste the entities in the entity directory. I keep trying, but, if you can provide a solution, I really appreciate that – Arefe Oct 14 '17 at 05:06
  • what for there are `ipAddressId` and `httpMessageId` in `IMAssociation` and why those have `@Id` annotation ? – pirho Oct 14 '17 at 07:48
  • I have tried to map with the field name mistakenly then to the column names. Its mentioned earlier from the other comment as well – Arefe Oct 14 '17 at 07:50

2 Answers2

3

This is your problem: https://hibernate.atlassian.net/browse/HHH-6044

Similar post in SO: Hibernate @GeneratedValue in a composite key

You can have 3 options here:

  1. You can still implement a composite key but you have to remove the auto-generated Id in your IMAssociation entity. Your composite key would only be composed of 2 properties - ipAddressId and httpMessageId which are derived/foreign keys from your Address and Message entities, respectively.
  2. Or you implement a single primary key: here you can use id auto-generation via IDENTITY strategy, but you have to remove the other id fields you declared - ipAddressId and httpMessageId.
  3. If you still want to keep the 3 Id fields - id, ipAddressId and httpMessageId, then remove id auto-generation (@GeneratedValue annotation) and manually assign an id value to your IMAssociation entity.
Ish
  • 3,992
  • 1
  • 17
  • 23
  • 1
    I have tried the 1st option and get some error. I have provided the error stack in the updated question. I really appreciate if you can provide a working solution as asnwer – Arefe Oct 14 '17 at 07:57
0

It might be that there is no need for ipAddressId and httpMessageId I think that columns holding those values are generated automatically in dblevel by @PrimaryKeyJoinColumn annotations and the value can be accessed in IMAssociation by ip.getMessage() and ip.getId().

pirho
  • 11,565
  • 12
  • 43
  • 70