3

Here is my code. I would like to generate an automatic ID based on parent class. I'm using a method to create Airport, so my ID it's coming with is a null value. ID in AirportModel will be generated, but I don't know how to make it in child class.

@Entity(name = "Airport")
@Table(name = "ai_airport")
public class AirportModel {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "airport_id")
    private List<AirportTranslatedModel> translations;

Second class(child):

    @Entity(name = "AirportTranslated")
    @IdClass(AirportTranslatedModelKey.class)
    @Table(name = "ai_translated_airport")
    public class AirportTranslatedModel 
        @Id
        @Column(name="airport_id")
        private Long airportId;

        @Id
        @Column(name="language_code", length=2)
        private String languageCode;

Third one(keys):

@Embeddable
public class AirportTranslatedModelKey implements Serializable {

    @Column(name="airport_id")
    private Long airportId;

    @Column(name="language_code", length=2)
    private String languageCode;

I still got the same errors; log:

Hibernate: insert into ai_airport (active, airport_code, city_code, country_code, externa
l_id, is_default, latitude, longitude, market_code, min_connection_time_DD, min_connection_time_DI, min_connection_time_id, min_connection_time_II, time_diff, VERSION) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Hibernate: insert into ai_translated_airport (airport_long_name, airport_short_name, airp
ort_id, language_code) values (?, ?, ?, ?)
ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Column 'airport_id' cannot be null
Marcin Erbel
  • 1,597
  • 6
  • 32
  • 51
  • What's the question? You want `airportId` in `AirportTranslatedModelKey` to be the generated `id` from `AirportModel`? – DannyMo Jun 14 '13 at 15:33
  • i think this link will help http://stackoverflow.com/questions/2611619/onetomany-and-composite-primary-keys – Gopal Jun 14 '13 at 15:44

1 Answers1

1

Your current setup has the AirportTranslatedModel airport_id field mapped through a Long- you will need to set the airportId manually to have it set the id in the database. This will likely require that you persist AirportModel and possibly flush to have its PK assigned and available prior to making the AirportModel->AirportTranslatedModel association, so that you can then set the AirportTranslatedModel.airportId.

JPA 2 though allows derived Ids. If you want AirportTranslatedModel to have its ID assigned from AirportModel, it needs to have a relationship to it. There is a simple example at http://wiki.eclipse.org/EclipseLink/Examples/JPA/2.0/DerivedIdentifiers

If you were to model your classes in a similar fashion, it might look like:

public class AirportModel {
  ..
  @OneToMany(mappedby="airportModel", cascade = CascadeType.ALL, orphanRemoval = true)
  private List<AirportTranslatedModel> translations;
  ..
}

public class AirportTranslatedModel {
  @Id
  @JoinColumn(name="airport_id")
  private AirportModel airportModel;

  @Id
  @Column(name="language_code", length=2)
  private String languageCode;
  ..
}
public class AirportTranslatedModelKey implements Serializable {
    private Long airportModel;

    private String languageCode;
}

Notice that there is no need to make the AirportTranslatedModelKey and embeddable if you are just using it as a pk class. Also note that the AirportTranslatedModelKey contains a Long airportModel - this must match the type of the pk in the AirportModel, and the name of the relationship property in AirportTranslatedModel.

This will allow AirportTranslatedModel to pull the airport_id value from AirportModel and use it as its PK even though it might not have been generated yet when both entities are still new.

Chris
  • 20,138
  • 2
  • 29
  • 43
  • I'm sorry, but I'm not sure that I understand. Is it AirportTranslatedModelKey necessery in this sollution? – Marcin Erbel Jun 17 '13 at 08:33
  • Yes, I just didnt cut and paste your entity and idclass annotations. It is needed only for find operations on AirportTranalationModel – Chris Jun 17 '13 at 12:04
  • I still got the same errors. I put it in my question. I think that problem is in fact that I'm trying to set primary key which is null. When exactly primary key from parent table is going to be generate? – Marcin Erbel Jun 19 '13 at 15:39
  • If you are not using JPA's derived IDs to set the ID for you as in the example, then you need to have the pk set in AirportModel before you can pass it in to AirportTranslatedModel. You need to persist AirportModel and likely flush to have the ID assigned before you can create and persist AirportTranslatedModel. When it gets assigned is up to your persistence provider and generation type - some can preallocate sequences on persist, while some will have to be assigned on flush/commit. – Chris Jun 19 '13 at 18:10