分类 Java 下的文章

As an answer to okutane, please see snippet:

@JoinColumn(name = "car_id", insertable = false, updatable = false)
@ManyToOne(targetEntity = Car.class, fetch = FetchType.EAGER)
private Car car;

@Column(name = "car_id")
private Long carId;

So what happens here is that when you want to do an insert/update, you only populate the carId field and perform the insert/update. Since the car field is non-insertable and non-updatable Hibernate will not complain about this and since in your database model you would only populate your car_id as a foreign key anyway this is enough at this point (and your foreign key relationship on the database will ensure your data integrity). Now when you fetch your entity the car field will be populated by Hibernate giving you the flexibility where only your parent gets fetched when it needs to.

answered by 'Jonck van der Kogel'

JPA many-to-one relation - need to save only Id

Java TypeJava DB, Derby, CloudScapeOracleDB2SybaseMS-SQL ServerMySQL Server
boolean, java.lang.BooleanSMALLINTNUMBER(1)SMALLINTBITBITTINYINT(1)
int, java.lang.IntegerINTEGERNUMBER(10)INTEGERINTEGERINTEGERINTEGER
long, java.lang.LongBIGINTNUMBER(19)INTEGERNUMERIC(19)NUMERIC(19)BIGINT
float, java.lang.FloatFLOATNUMBER(19,4)FLOATFLOAT(16)FLOAT(16)FLOAT
double, java.lang.DoubleFLOATNUMBER(19,4)FLOATFLOAT(32)FLOAT(32)DOUBLE
short, java.lang.ShortSMALLINTNUMBER(5)SMALLINTSMALLINTSMALLINTSMALLINT
byte, java.lang.ByteSMALLINTNUMBER(3)SMALLINTSMALLINTSMALLINTSMALLINT
java.lang.NumberDECIMALNUMBER(38)DECIMAL(15)NUMERIC(38)NUMERIC(28)DECIMAL(38)
java.math.BigIntegerBIGINTNUMBER(38)BIGINTNUMERIC(38)NUMERIC(28)BIGINT
java.math.BigDecimalDECIMALNUMBER(38)DECIMAL(15)NUMERIC(38)NUMERIC(28)DECIMAL(38)
java.lang.StringVARCHAR(255)VARCHAR(255)VARCHAR(255)VARCHAR(255)VARCHAR(255)VARCHAR(255)
char, java.lang.CharacterCHAR(1)CHAR(1)CHAR(1)CHAR(1)CHAR(1)CHAR(1)
byte[], java.lang.Byte[], java.sql.BlobBLOB(64000)LONG RAWBLOB(64000)IMAGEIMAGEBLOB(64000)
char[], java.lang.Character[], java.sql.ClobCLOB(64000)LONGCLOB(64000)TEXTTEXTTEXT(64000)
java.sql.DateDATEDATEDATEDATETIMEDATETIMEDATE
java.sql.TimeTIMEDATETIMEDATETIMEDATETIMETIME
java.sql.TimestampTIMESTAMPDATETIMESTAMPDATETIMEDATETIMEDATETIME
Supported Data Types (Sun Java System Application Server Platform Edition 9 Developer's Guide)

background

I made an embed page with Spring-boot. The one to show from other sites with iframe. At this time, Spring Security was introduced and the HTTP header "X-Frame-Options" was set to DENY by default, and the embedded page was not displayed.

Since this X-Frame-Options itself should be DENY in order to suppress clickjacking on ordinary pages, I decided that it is better to set not to send this header only on the embedding page. However, "setting X-Frame-Options only for a specific URL" was a little complicated, so I summarized it.

I think that other HTTP Headers can be set for each URL if necessary (unverified).

- 阅读剩余部分 -

2.MD5 Using MessageDigest Class

java.security.MessageDigest class:

MessageDigest.getInstance(String Algorithm)
public void update(byte [] input)
public byte[] digest()

generates a hash for a password and then verifies it:

@Test
public void givenPassword_whenHashing_thenVerifying() 
  throws NoSuchAlgorithmException {
    String hash = "35454B055CC325EA1AF2126E27707052";
    String password = "ILoveJava";
        
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(password.getBytes());
    byte[] digest = md.digest();
    String myHash = DatatypeConverter
      .printHexBinary(digest).toUpperCase();
        
    assertThat(myHash.equals(hash)).isTrue();
}

- 阅读剩余部分 -