简体   繁体   中英

String as Entity ID inserted by user [EclipseLink]

[INTRO]

  1. Database : Apache Derby
  2. JPA : EclipseLink

Hey, I was looking for answer but couldn't find any so here it goes. I'm writing simple sudoku app, and the next feature which I would like to add is saving my Sudoku boards in database, and retrieve them when it's needed. Here is UML diagram of my main two classes: SudokuBoard.uml The structure of my two entities are as follows :

The SudokuBoard entity:

@Entity
public class SudokuBoard implements Serializable, Cloneable {

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

private static final long serialVersionUID = 1L;

@OneToMany(cascade=CascadeType.PERSIST)
private ArrayList<SudokuField> board;

public ArrayList<SudokuField> board() {
    return board;
}

public void setBoard(ArrayList<SudokuField> board) {
    this.board= board;
}

public Long etId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
} 

The SudokuField entity:

@Entity
public class SudokuField implements Serializable, Comparable<SudokuField>, 
Cloneable {

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

@Transient
private static final Logger logger = 
LoggerFactory.getLogger(SudokuField.class);
@Transient
private static final long serialVersionUID = 1L;

@Basic(fetch = FetchType.EAGER)
@Column(name = "Value")
private int value;

@ManyToOne
@JoinColumn(name = "board_fk", referencedColumnName = "id")
private SudokuBoard sudokuBoard;

The execution in DAO:

@Override
public void write(SudokuBoard obj, String path) throws 
FileNotFoundException, IOException {
    entityManager.getTransaction().begin();
    entityManager.persist(obj);
    entityManager.getTransaction().commit();
}

[PROBLEM]

  1. I wonder if there is any possiblity to not use auto generated key but instead use String as PK in SudokuBoard entity. In my application I implemented binding so I would like to save the same object of SudokuBoard which is changing over time under different names.

Hope I stated my intentions clearly. Thanks for any help and tips how it could be done.

Using String as primary key is straightforward - just declare it as such and drop the @GeneratedValue annotation.

However, changing the primary key (if that's what you mean by 'saving the same object (...) under different names') is not possible. If you try to persist/merge an existing entity under a different primary key, JPA will either raise an exception or treat it as a new entity, resulting in duplicate entries in the database. Simply put, assigning id to an entity is permanent.

All in all, I'd suggest you keep an autogenerated surrogate key and declare another unique String field for the name. Conveying business information using technical fields is rarely a good idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM